in Education by
Here is the code class method { int counter = 0; public static void main(String[] args) { System.out.println(counter); } } It gave me this error. Main.java:6: error: non-static variable counter cannot be referenced from a static context System.out.println(counter); ^ How do I make it recognize the variables of the class? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
A reference cannot be made from a static to a non-static method. To make it clear, go through the differences below. Static variables are class variables which belong to the class with only one instance created initially. Whereas, non-static variables are initialized every time you create an object for the class. You have not created an object hence non-static does not exist and static always do that is why an error is shown Try the given statements instead: Object instance = new Constuctor().methodCall(); or primitive name = new Constuctor().methodCall();

Related questions

0 votes
    Here is the code class method { int counter = 0; public static void main(String[] args) { System.out. ... ),Core Questions, Core Hadoop MCQ,core interview questions for experienced...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    What is the meaning of _ after for in this code? if tbh.bag: n = 0 for _ in tbh.bag.atom_set(): n += 1 Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Coming from a C# background the naming convention for variables and method names are usually either Camel Case or ... for Python? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I've been given to understand that Python is an interpreted language... However, when I look at my Python source ... do these come in? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I'm trying to use string interpolation on my variable to reference another variable: // Set up variable and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    Static member function cannot invalid non static member function Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    Drop Table cannot be used to drop a table referenced by a _________ constraint. (a) Local Key (b) ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Which of these modifiers can be used for a variable so that it can be accessed from any thread or parts of a ... O & Applets of Java Select the correct answer from above options...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Can abstract keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization ... Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of the following classes can catch all exceptions which cannot be caught? (a) RuntimeException (b) Error (c ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these class is related to all the exceptions that cannot be caught? (a) Error (b) Exception (c ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these types cannot be used to initiate a generic type? (a) Integer class (b) Float class (c) ... in section Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these instance cannot be created? (a) Integer instance (b) Generic class instance (c) Generic type ... Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    What happens if IP Address of host cannot be determined? (a) The system exit with no message (b) ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Using which annotation non visible or private method can be tested? (a) @VisibleForTesting (b) @NonVisibleForTesting (c ... of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
...