• Register
1 vote
27 views

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? 

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

In your code you need to declare the instance variables static or you should refer them using an object in the method, so you should try like this:

public class StaticDemo {
    int i = 0;
    public static void main(String[] args) {
        System.out.println("in main method which is static");
        StaticDemo obj = new StaticDemo();
        obj.i = 5;
        obj.instanceMethod();
    }
    
    public void instanceMethod(){
        System.out.println("Value of i- " + i);
    }
}

Hope this help you. 

11 6 4
34,950 points

Related questions

1 vote
1 answer 6 views
6 views
Problem; Getting the following error. Java non static variable this cannot be referenced from a static context.
asked Mar 29 PkGuy 23.5k points
0 votes
1 answer 103 views
103 views
Problem : Getting bellow error "non static method cannot be referenced from a static context java"
asked Nov 5, 2019 peterlaw 6.9k points
0 votes
1 answer 91 views
91 views
Problem : In Java, I want to have something as below : class MyClazz<T> {  static void doIt(T object) {    // More code  } } But I face below error &ldquo;Cannot make a static reference to the non-static type T&rdquo; I am unable to understand ... about a subject. Could someone clarify if such the use is possible, by the similar manner? Also, why was my original attempt went unsuccessful?
asked Jan 21, 2020 jwilliam 3.9k points
0 votes
1 answer 8 views
8 views
Problem: Could someone please explain to me in really simple terms what Static or Class Methods are?
asked Apr 22 ummeshani 9.5k points
0 votes
2 answers 26 views
26 views
Problem: My question is regarding static methods. Why can the static method not be overridden? Moreover, can we overload static methods? Why? Why not? Please explain in as simple words as possible. Thanks!
asked Mar 2 Code Learner 9.8k points
0 votes
1 answer 5 views
5 views
Problem: I am stuck during learning as I have already mentioned in my question .someone please cooperate me to continue my project:main.this cannot be referenced from a static context.Thanks
asked Apr 9 ashik 14.6k points
0 votes
1 answer 11 views
11 views
solve this question.
asked Jan 20 TeamScript 13.5k points
0 votes
1 answer 8 views
8 views
Problem: What are the desirable situation (real-life examples) to create static methods except for creating helper? Can anybody answer this question by briefly explaining every scenario???
asked Mar 23 ummesalma 29.2k points
0 votes
1 answer 2 views
2 views
Problem: I am stuck now and need some suggestions > Non-static method tostring() cannot be referenced from a static context
asked 2 days ago ummeshani 9.5k points
1 vote
1 answer 4 views
4 views
Problem: Can someone please help fix this uncaught error : java non static method cannot be referenced from a static context
asked Mar 26 MUHAMMAD MUNEEB 83k points