Im still new to java and i tried to create a inner class and call the method inside main. But theres a compilation error saying \"Non static variable - This cannot be refere
If you are novice to Java, next example may be helpful for you additionally.
"main()" is unsuitable for any complex logic. You cannot call from it any method that is not static in class. "main()" is nessesary only for starting of application,
In many cases first of all you need to create instance of class, that contains method "main". In example it is "OuterClass".
When instance of "OuterClass" exists, you have no problem to call from it anything dynamic, like your InnerClass methods of InnerClass object.
Here is an example:
public class OuterClass {
public static void main(String args []){
new OuterClass(); // Instance of First class
}
public OuterClass () { // constuctor
InnerClass myObject = new InnerClass();
myObject.newMethod();
}
public class InnerClass{
public void newMethod(){
System.out.println("Second class");
}
}
}