Notes about objects

Constructors

So this Dog(String name) { } is a constructor, but void Dog(String name) { } is not.

Static

"static" + variable

  • "static" + method

    "static" + class

    ONLY with an inner class. This means that you don't have to create an instance of the outer class in order to access the inner class. Otherwise you do.

    Final

    "final" + variable

    "final" + method

    "final" + class

    Access Modifiers

    private variable or method

    Inner Class Example

    class MainClass{
       public static void main(String[] args) {
          int z = InnerClass.aaa; //no error when outer class accesses this
       }
    
       private static int bbb = 18; //private variable in main class
    
       static class InnerClass{
           
           private static int aaa = 19; //private variable in inner class
           
           InnerClass() {
              bbb = 20; //no error when inner class accesses this
           }
       }
    }
    

    "package private" variable or method or class

    protected variable or method

    public variable or method or class

    Classes can only be public or nothing.
    If you have a private class on its own as a top-level class, then you can't get access to it from anywhere!
    Inner or Nested classes can be private which means that access is restricted to the scope of that outer class.

    public
    The class that starts your program must be public and it must have a public method called main().
    Actually, it has to be written specifically as public static void main(String[] args) {}

    Abstract

    "abstract" + class

    "abstract" + method