You are not logged in. Please login at to post your questions!


problem with object in java

What about the objects which we define outside static method ? how to use them ?
Example:=> In the following code how to use object "obj1". I know "obj1" can't be used in static method. But it can be used in non-static method. So, How to use "obj1" in method "nonstatic" ?


class Test {   
    int a,b;  
    String name;  
    Test obj1=new Test();
    public void nonstatic(){}
    public static void main(String[] args){  
        System.out.println("How to use obj1");
This question is marked "community wiki".

asked 04 Feb '14, 19:25

ayush1993's gravatar image

accept rate: 0%

edited 04 Feb '14, 20:00

Why are you asking same question again? You cannot use instance variables in static method (where you have no instance)...

That's what compiler is trying to tell you when you do something like


in main()...

Cannot make a static reference to the non-static field obj1


answered 04 Feb '14, 19:32

betlista's gravatar image

3★betlista ♦♦
accept rate: 11%

edited 04 Feb '14, 19:35

Can I use object "obj1" in non-static method ? If yes, How ?

(04 Feb '14, 19:52) ayush19930★

I answered it here - You have to create instance and then you can use non-static methods on that instance...

(04 Feb '14, 20:04) betlista ♦♦3★

Initialize the using the static keyword before the type. When you initialize objects or primitive variables as global variables you have to add the keyword static if you want to use them in static methods, otherwise you will only be able to use them in non-static methods. Note that you can still use static variables in non-static methods, it just doesn't work the other way around. Are you picking up Java now or did you just start programming? Either way you should check this link to understand the basics, even if you just switched languages, it doesn't hurt to spare some time reviewing these things.


answered 04 Feb '14, 19:47

junior94's gravatar image

accept rate: 15%

In the above code How can I use Object "obj1" in non-static method ??

(04 Feb '14, 19:54) ayush19930★

Nice link I have to check it too ;-)

(04 Feb '14, 20:05) betlista ♦♦3★

@ayush1993, check @betlista's answer... Are you a novice in programming or just in Java? Having that information might be useful for us to provide better answers and guide you in the right direction...

(04 Feb '14, 21:33) junior944★

@junior94, you still not got my question. See I have modified my question and then try to answer it.

(04 Feb '14, 23:20) ayush19930★

i should say make on object of class Test and then try to access obj1....might work


answered 04 Feb '14, 21:41

bradley's gravatar image

accept rate: 20%

@bradley, Now you got my my question.I tried it but got error.

(04 Feb '14, 23:18) ayush19930★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 04 Feb '14, 19:25

question was seen: 1,512 times

last updated: 04 Feb '14, 23:20