5 Replies - 1831 Views - Last Post: 01 February 2013 - 05:35 PM Rate Topic: -----

#1 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 08-September 12

Passing objects between classes.

Posted 01 February 2013 - 03:24 AM

I have 3 classes in total.

Run.java
- the java class which contains the main method to run the other 2 classes.
public static void main(String[] args)
{toGUI tg = new toGUI();
}


toLogic.java
- no main method, needs to pass methods to toGUI.java

toGUI.java
- no main method. Created an instance of toLogic class private toLogic tlg; and in the toGUI constructor I added in parameters public toGui(toLogic tlg){ }; to accept methods from toLogic class.

However, now the problem is that the main method of Run.java doesn't execute as the constructor of toGUI class has parameters. How do I code the main method in Run.java to execute the object of toGUI class ?

Thanks.

This post has been edited by Gisengryl: 01 February 2013 - 03:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing objects between classes.

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Passing objects between classes.

Posted 01 February 2013 - 03:49 AM

Can you please post your code over here so that we will be in a better position to help you out.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Passing objects between classes.

Posted 01 February 2013 - 04:21 AM

References to other class instances can be passed at least 2 ways:

1. through the class' constructor
public class SomeClass
{
   // instance variables
   OtherClass otherClass;

   // constructor
   public SomeClass( OtherClass otherClass )
   {
      this.otherClass = otherClass;

       . . .
   }

The constructor someClass is called from a class that has the desired reference to an OtherClass instance, and when the SomeClass instance is created, a reference to the OtherClass is contained in the instance variable otherClass.

2. if an instance of SomeClass already exists in OtherClass, use a setter:
public class SomeClass
{
   // instance variables
   OtherClass otherClass;

   // setter
   public void setOtherClass( OtherClass otherClass )
   {
      this.otherClass = otherClass;

       . . .
   }

The call to setOtherClass() from OtherClass would be:

someClass.setOtherClass( this );

There are probably other ways, but these are the most common.
Was This Post Helpful? 1
  • +
  • -

#4 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 08-September 12

Re: Passing objects between classes.

Posted 01 February 2013 - 05:11 AM

View Postraghav.naganathan, on 01 February 2013 - 03:49 AM, said:

Can you please post your code over here so that we will be in a better position to help you out.

regards,
Raghav


Hopefully this helps :)

public class toLogic  {

	// methods to be passed to toGUI
}

public class toGUI{
	// instance of toLogic class 
	private toLogic tlg;

	// constructor
	public toLogic(toLogic tlg){
	}	
}

public class Run {
	public static void main(String[] args){
	// doesn't work as there is no empty contructor
	// in toGui anymore
    toGui tg = new toGui(); 
}


@ gregBrannon :

What does the this.otherClass = otherClass; do ? Sorry if this is a really dumb question to ask.

What I am doing so far seems to be the 1st method you typed
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 866
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: Passing objects between classes.

Posted 01 February 2013 - 07:15 AM

View PostGisengryl, on 01 February 2013 - 10:24 AM, said:

However, now the problem is that the main method of Run.java doesn't execute as the constructor of toGUI class has parameters. How do I code the main method in Run.java to execute the object of toGUI class ?

Thanks.


public static void main(String[] args) {
    ToGui gui = new ToGui("Pizza", 300, 200);



public class toGui {
private String foodWantedForDinner;
private int widthofWindow;
private int heightOfWindow;

    //constructor
    public toGui(String foodWeWantForDinner, int width, int height) {
        foodWantedForDinner = foodWeWantForDinner;
        widthOfWindow = width;
        heightOfWindow = height;
    }//end constructor.
}//end ToGui class


Was This Post Helpful? 0
  • +
  • -

#6 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 08-September 12

Re: Passing objects between classes.

Posted 01 February 2013 - 05:35 PM

@ farell2k : Hmm so in my Run Class, I should do this ?

public class Run {
	public static void main(String[] args){
	// doesn't work as there is no empty contructor
	// in toGui anymore
	toLogic tlg = new toLogic();		
    toGui tg = new toGui(tlg); 
}


And then place all the methods I want transferred from toLogic class to toGUI class in the constructor of toLogic class ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1