Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld >> Java Beginner

Pages: 1
chache
stranger


Reged: 08/18/08
Posts: 1
Class call (between objects)
      #181900 - 08/18/08 09:11 PM

I need some advice on class call. I have a main class that do the major things. There is a method that will create an object from another class (in the same package). Here is the problem, can this object refer to variables in the main class? If it is possible, what shall I do in that class.

Post Extras: Print Post   Remind Me!   Notify Moderator  
kafrlust
journeyman


Reged: 05/31/06
Posts: 81
Loc: Greece
Re: Class call (between objects) [Re: chache]
      #181917 - 08/19/08 03:22 AM

Yes it is!!One solution to your problem could be this

Code:
 
public class MainClass{
private int var1
private int var2
public void method(){
AnotherObject obj=new AnotherObject()
obj.setValue1(var1)
obj.setValue2(var2)
}
}
public class AnotherObject{
private int value1
private int value2
public void setValue1(int value){
value1=value
}
public void setValue2(int value){
value2=value
}
//other methods go here
}



--------------------
reach whatever you can't reach(N.Kazantzakis)


Post Extras: Print Post   Remind Me!   Notify Moderator  
hiwa
Carpal Tunnel


Reged: 06/21/03
Posts: 7704
Loc: Japan
Re: Class call (between objects) [Re: chache]
      #181931 - 08/19/08 06:36 AM

Because of the persistent bugs of the forum software see the URL below:
http://homepage1.nifty.com/algafield/answer.txt

--------------------
*stop cruelty* Annual number of euthanized cats&dogs: US 5M, JP 500K.*for our better karma*


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 3 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 5006

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5