×

# problem with object in java

 0 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" ? Code:=> 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 10●1●4●5 accept rate: 0%

 2 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  obj.a  in main()... Cannot make a static reference to the non-static field obj1 answered 04 Feb '14, 19:32 16.9k●49●115●225 accept rate: 11% Can I use object "obj1" in non-static method ? If yes, How ? (04 Feb '14, 19:52) I answered it here - http://discuss.codechef.com/questions/37959/a-doubt-in-java You have to create instance and then you can use non-static methods on that instance... (04 Feb '14, 20:04)
 1 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 4★junior94 3.2k●14●30●58 accept rate: 15% In the above code How can I use Object "obj1" in non-static method ?? (04 Feb '14, 19:54) 1 Nice link I have to check it too ;-) (04 Feb '14, 20:05) @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)
 0 i should say make on object of class Test and then try to access obj1....might work answered 04 Feb '14, 21:41 3★bradley 656●2●3●21 accept rate: 20% @bradley, Now you got my my question.I tried it but got error. (04 Feb '14, 23:18)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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:

×2,286
×1,254
×808
×533
×243
×8

question asked: 04 Feb '14, 19:25

question was seen: 1,491 times

last updated: 04 Feb '14, 23:20