Classes

The class keyword defines class. The class name and the file name where the class is declared must match. Obviously, only one top-level class is allowed per file. Java has a convention for naming classes.

By default class and its members have a package level scope. The public keyword makes the class or member of the accessible from outside the package.

The new operator creates a new instance of class.

The . operator is used to access to the members from outside of class.

// syntax
/* class <class-name> {
 ... // field and method declarations
}
*/

/**
* An example of a simple class declaration.
* @author socode4.com
*/
public class Demo {

    public int field1 = 1;
    String field2 = "demo";

    public void method1(int a){
        System.out.println("You are passed "+a
          + " as argument to method method1.");
    }

    public static void main(String[] args) {
        Demo demo = new Demo();  
        
        demo.field1 = 34;
        System.out.println("field1 = "+demo.field1);
        
        demo.method1(45);
    }
}