• Register
1 vote
22 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

0 votes
1 answer 86 views
86 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 11 views
11 views
Problem: I've written this test code: class MyProgram { int count = 0; public static void main(String[] args) { System.out.println(count); } } But it gives the following error: Main.java:6: error: non-static variable count cannot be referenced from a static context System.out.println(count); ^ How do I get my methods to recognize my class variables?
asked Feb 2 Ali 4.4k points
0 votes
1 answer 12 views
12 views
Problem: A reference cannot be made from a static to a non-static method. To make it clear, go through the differences Static variables are class variables which belong to the class with only one instance created initially. Whereas, How non-static variables are initialized every time you create an object for the class.
asked Feb 13 Ali 4.4k points
0 votes
1 answer 6 views
6 views
solve this question.
asked Jan 20 TeamScript 13.5k points
0 votes
1 answer 58 views
58 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 13 views
13 views
Why? I am not asking for solutions.
asked Jan 3 TeamScript 13.5k points
0 votes
1 answer 12 views
12 views
Solve this question.
asked Jan 22 TeamScript 13.5k points
0 votes
1 answer 20 views
20 views
How do I get my methods to recognize my class variables?
asked Dec 28, 2020 TeamScript 13.5k points
0 votes
1 answer 26 views
26 views
How do I get my methods to recognize my class variables?
asked Dec 28, 2020 TeamScript 13.5k points
0 votes
1 answer 28 views
28 views
I want to make a static reference to the non-static method.
asked Sep 22, 2020 Daniel Anderson 4k points