• Register
Log In
0 votes

Problem :

Getting bellow error
"non static method cannot be referenced from a static context java"
6.9k points

1 Answer

0 votes

Solution :

One can not make the reference to a Static variable from a NonStatic method. 

Static Variables are called class variables as they belong to the class and they have only one instance which is created at the begining only. 
NonStatic Variables need to be initialized every time one creates an object of that Class.

When one uses new() keyword to create a copy of every NonStatic filed for every object, but it is not the case for Static variables. 
So it gives compile time error if you are refering a Static variable from a NonStatic method.
36.1k points

Related questions

1 vote
1 answer 13 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 LizzyM 5.7k points
2 votes
1 answer 16 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 Gavin 15k points
0 votes
1 answer 18 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 LizzyM 5.7k points
0 votes
1 answer 26 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 ... System.out.println("Value of i- " + i); } } Can anyone givr me details about it, this would help me out
asked Mar 21 LizzyM 5.7k points
0 votes
1 answer 33 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 jwilliam 3.9k points