• Register
0 votes
89 views

Problem:

I tried to referring a variable using static reference implies to referring using the class name But, to access instance variables it is must to create an object, these are not available in the memory, before instantiation. Therefore, i cannot make static reference to non-static fields(variables) in Java. If i still, try to do so a compile time error is generated saying “non-static variable math cannot be referenced from a static contex.  My problem is given bellow:
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);
    }
}

Can anyone givr me details about it, this would help me out

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes

Answer: 

To make your program work, either you need to declare the instance variables static or, we should refer them using an object in the method 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);
    }
}

 

11 6 4
34,950 points

Related questions

0 votes
1 answer 58 views
58 views
Problem: I am new to java programming. I was doing my coding practice. I wrote a code: public class JavaHelloWorld { public static void main(String args[]) { sayHello(); } public void sayHello() { System.out.println("Hello world from java2blog"); } But ... make static reference to non static method sayHello form the type JavaHelloWorld Why am I getting this type of error message, Can anyone help?
asked Mar 23, 2020 LizzyM 6.1k points
0 votes
1 answer 39 views
39 views
I want to make a static reference to the non-static method.
asked Sep 22, 2020 Daniel Anderson 4k points
2 votes
1 answer 93 views
93 views
Problem: Learning a new programming language is really fun. So I am doing now. Yeah, java is fun but I stuck somewhere in static reference to the non-static method. What is this? I couldn’t figure it out properly. I have written a program below for ... public void mySecondMethod(){ } } I just tried to call one of my functions inside another function. Any solution is much appreciated. Thanks
asked Mar 25, 2020 Gavin 15.3k points
1 vote
1 answer 3 views
3 views
Problem: Need help for the following: Cannot make a static reference to the non-static field java.
asked 4 days ago 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
1 vote
1 answer 27 views
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 ... -static method instanceMethod() cannot be referenced from a static context instanceMethod(); ^ 3 errors Can anyone help me out to fix this problem?
asked Apr 7, 2020 LizzyM 6.1k points
0 votes
1 answer 11 views
11 views
Problem: i get error "An object reference is required for the non-static field, method, or property 'text1.Program.method(int, int)'" error An object reference is required for the non-static field, method, or property
asked Dec 3, 2020 Mashhoodch 13k points
0 votes
1 answer 5 views
5 views
Question: I'm getting an error when I try to call a non-static method in a static class. Cannot make a static reference to the non-static method methodName() from the type playback I can't make the method static as this gives me an error too. This static ... any way to get round calling an non-static method in another static method? (The two methods are in seperate packages and seperate classes).
asked Apr 13 Atik03 26.5k points
0 votes
1 answer 7 views
7 views
Problem: I have problem with the "getClass", the eclipse writing this messeage: "cannot make a static reference to the non-static method getClass() from the type Object" This is the code: public static void main(String[] args) { JFrame f = new JFrame(); File path = new File(getClass().getResource("/resources/image.jpg").getFile()); BufferedImage image = ImageIO.read(path); Thank you!
asked Apr 25 muktaa 34.4k points
0 votes
1 answer 29 views