expected Error in java Compilation [duplicate]

well in class level you can only define attributes of that class, cant do any processing which you are doing in classA and classB. Processing can only be done in method.

Just add main method make objects there

public class Hello
{
    // value / method
    public static String staticValue;
    public String nonStaticValue;

    public void main(String[] args){

      Hello hello = new Hello();
      Hello.staticValue = "abc";
      hello.nonStaticValue = "xyz";

      Hello hello2 = new Hello(); // here staticValue = "abc"
      Hello.staticValue; // will have value of "abc"
      hello2.nonStaticValue; // will have value of null
    }
}

Main method is entry point of any program in java. Dont worry if you are confused where this main method is called.

Leave a Comment