Problem:
I am very noob in java programming. I was doing my coding practice and wrote this code:
public class StaticDemo {
int i = 0;
public static void main(String[] args) {
System.out.println("in main method which is static");
// Trying to access non-static field
i = 5;
// Trying to access non-static method
instanceMethod();
}
public void instanceMethod(){
System.out.println("Value of i- " + i);
}
}
but the program gives me he following error message:
Main.java:1: error: class StaticDemo is public, should be declared in a file named StaticDemo.java
public class StaticDemo {
^
Main.java:6: error: non-static variable i cannot be referenced from a static context
i = 5;
^
Main.java:8: error: non-static method instanceMethod() cannot be referenced from a static context
instanceMethod();
^
3 errors
Can anyone help me out to fix this problem?