I have learned that fields are like global variables which can be accessed by the methods inside the same class. I have done it this way before and never had a problem. I have now a class where I have some fields but the methods cannot access them without having to make them static fields. I get the error "cannot make static reference to non-static..." I thought static was to access fields on other classes without having to create an object reference to the class. The only difference I have with this code is that I have a single class and my main() method within this class. Does having main() inside this class make a difference?
-
Can you post your code and point out where the problem is? `static` data means it's associated with the class, not a specific *instance* of the class. – Mike Christensen Apr 18 '14 at 03:43
-
It's very difficult assume code and problem by just reading the theory. – Gundamaiah Apr 18 '14 at 03:47
3 Answers
A static member only exists once for the class itself as opposed to regular class members which are distinct per instance of your class.
Having a main() method does not impact the behavior of your static members, however static methods can only access static members while non-static methods can access both static and non-static class members.

- 12,305
- 2
- 26
- 27
-
Thank you, I made the mistake of making my methods static. That's what was causing the problem. – polaris Apr 18 '14 at 03:46
-
"static methods can only access static members" - Not entirely correct; in order to access ***instance*** members, you must pass them a object reference as a parameter. While there are legitimate use-cases for this, it would be a design smell that the method should be an instance method (and perhaps should be reframed in that context). – Lawrence Dol Apr 18 '14 at 03:53
-
1@LawrenceDol yes, but far too pedantic given the scope of the question and implied experience level of the submitter. – caskey Apr 18 '14 at 03:54
-
i think there is some issue with the understanding of static. i guess based on the design the method signature and its impl has to be decided. form what the OP has replied i think he is not sure about the design. just changing static to non static to make something work is not correct way. so i guess he needs to understand the concept first and later impl will be easy :) – vikeng21 Apr 18 '14 at 04:07
-
@vikeng21 you are correct, I don't fully understand the concept of static as I'm just learning Java. I find it somehow confusing when it is said that a static field belongs to the class and not to an instance of the class. A static variable will be affected every time it gets changed within a method and reflected later on at the other methods whereas a non-static won't show any changes caused by other methods if I understand correctly. Are there specific ways when I would rather chose one way or the other? – polaris Apr 18 '14 at 04:39
-
@polaris it happens a lot of times with static its very confusing at first so read a good tutorial before even touching the topic. we use static variable in a class when we need same value to be available to all objects of that class. you create static methods when u want to write utilities in you code.i mean method that does not depend on instance variables of a class but on arguments that we pass to method.the list goes on and is never ending with statics. its a tricky and interesting subject and google and SO are your sources. keep learning java statics :) – vikeng21 Apr 18 '14 at 06:26
You can't access non static instance inside static method. I think you are trying to access class variable inside main method directly, i.e.
class A
{
int x;
main() method
{
x;//Not accessible here,, create instance of class and access it.like
A a=new A();
a.x;
}
}

- 7,512
- 7
- 39
- 72
Static (methods,variables,classes, etc) belongs to Class not to the particular instance of the class. We define as static when the behaviour or state does not depend on any particular instance of the class. For Example "generate a Random Number " it does not depend on the instance, it always generate a number regardless of instance such behaviour can be defined as static.
Regarding the error, posting you code will helpful to give better solution.
Refer the below link to know more about Static and non-static
http://javarevisited.blogspot.in/2012/02/why-non-static-variable-cannot-be.html

- 112
- 2