-1

//Edited:I guess I represented the question wrong. I am familiar the concept that "we need an instance to access non static variables and methods".

But my question was

When I type the prgm in my eclipse it shows an error at line B but I am expecting to show even on A//

This is a basic question please don't mind.

       package com.sigma.java;
        import java.util.*;

        public class Test1 {

        private int a=1;
        public void add(){
         System.out.println(a);-------->A
          }

        public static void main(String[] args) {
        Test1 t=new Test1();
        System.out.println(a);-------------->B
          }
        } 

Compiler shows error in main method when trying print a(marked as B) (Error-Cannot make a static reference to the non-static field a) Question-Why dont I get the same error in add() method(marked as A)

  • 2
    Because `add()` is not `static` and `main()` is. Try printing `t.a`. – shmosel Apr 07 '17 at 00:54
  • you are calling a non static variable which is ` private int a=1;`from a static method which is `main()` method. just make `private static int a=1;` and you can see the difference. – Rajith Pemabandu Apr 07 '17 at 00:55
  • 1
    Please search your questions before asking as this one's been asked quite literally thousands of times before. If you feel that we really need one more, then search and find the prior canonical questions/answers first, show the fruits of this search in your question, and clearly state why you think that your question is different. – Hovercraft Full Of Eels Apr 07 '17 at 00:59
  • (1) Please format your code so it looks neat, and only include real code (or, if you want to write pseudocode, make sure you clearly label it as such). (2) "Edit" markers aren't generally necessary here, and often just bloat a question -- if anyone cares what the question used to ask, it's visible in the history, which you can see by clicking the link that says "edited [some time] ago" below your question, next to your profile picture and username. (3) No, you clearly don't understand the difference, because that difference is causing this error. Reread the answers to the linked question – Nic Apr 07 '17 at 03:42

3 Answers3

0

Variable a must be declared as static like bellow :

private static int a = 1 ;

You cannot use an instance variable in a static Java method.

0

Your main method should be:

Becuause, a is a instance variable and you need to use the instance t as you are using in a static method.

public static void main(String[] args) {
   Test1 t=new Test1();
   System.out.println(t.a);
}

In the add method, it is accessible because, it is member method where all the member variable can be accessed directory. Hence, it is valid and no error.

Rao
  • 20,781
  • 11
  • 57
  • 77
0

you can only call the add method if you have access to an instance of the class Test1. Each instance of the class Test1 has a copy of variable a.

However , you tried to directly /statically access the value of the string a from the main method. This is problematic because the call from the main method is not from an instance of Test1. The key point to remember is that the instance variable a is tied each instance of the class Test1

JavaPlanet
  • 83
  • 9