File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to correct the error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to correct the error" Watch "how to correct the error" New topic
Author

how to correct the error

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477



(1)In the above program get1() method is showing error that non-staticmethod get1() cannot referenced is static context.But my doubt is priya() method is not in a static context.How the error is coming?

(2)Is Objective HAS-A myObjective?

Pleased help me it is a very new concept for me.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


Signature of priya() method....



My dear friend, See line 1 and 2 in the method priya() . The get1() method is not a static method. How can you call by Objective.get1(). Make get1() method static. One more thing, when you will make get1() method static, then it can't refer myObjective variable defined in Objective class as non-static thing can't be used in static context so then you have to make myObjective static as well.


Naseem
[ July 21, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
If you do not want to change Objective61 class, then your Objective class should be changed to this:



Naseem
[ July 21, 2006: Message edited by: Naseem Khan ]
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Hi Friend,
I am glad to get your response. I have changed both to static. It was showing that error because in the Objective61 class main() method (which is static),we were calling the method priya() which is having Objective.get1().(that�s why we need the get1() method as static)please,I need to confirm my understanding?

(2)

class Objective {
static Objective myObjective;


Is Objective HAS-A myObjective? Please help me with has-a understanding too?
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

Is Objective HAS-A myObjective?


True

HAS-A means class is composed of other objects.

e.g.,



Mercedes IS-A Car (IS-A relationship with Car). Its a Car
Mercedes HAS-A relationship with Wheel, Cluches, Brake class as it has these things. It is composed of 4 wheels, brakes, cluches etc.

Naseem
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thank you so much Naseem. Another doubt on this same thing.

Objective s1= Objective.get1();
Objective s2= Objective.get1();

I thought s1 having (new Objective) and s2 having (new objective)created.How come this s1==s2 returns true when the two references refer to the different objects.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

Objective s1= Objective.get1();
Objective s2= Objective.get1();

I thought s1 having (new Objective) and s2 having (new objective)created.
How come this s1==s2 returns true when the two references refer to the different objects.


No. Two references s1 and s2 refer to the same object. Your myObjective variable defined in Objective class is static. Once you got an object and stored in myObjective static variable in next time when you call again get1() method your static variable is already referring to an instance of Objective class, so if statement will not be executed. If myObjective is null, then execute if.

Hope this clear your doubt.

Naseem
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Exactly Naseem.Thanks and have a great day!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to correct the error
 
Similar Threads
Statics
a question about Stack
OO, method calls in compile time
HAS-A problem
Static methods and inheritance.