Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Passing Objects to Constuctors

  1. #1

    Default Passing Objects to Constuctors

    We are learning about how to pass objects into constructors. In our class, we made this following code
    public class InventoryItem
    {
    	//fields
    	private String description;
    	private int units;
     
     
    	//Add New constructor
    	public InventoryItem(InventoryItem some_object)
    	{
    		description=some_object.description;
    		units=some_object.units;
    public void setDescription(String name_Item)
    	{
    		description=name_Item;
    	}
    public String getDescription()
    	{
    		return description;
    	}
    	}
    As you can see the object of the same class is passed as the argument. Inside the constructor, our teacher did
    some_object.description
    This constructor, from my understanding, copies the description field and unit field to an object to the new object of class.

    Meanwhile, here is the demo class.
    public static void main(String[] args)
    	{
    		//Create object
    		InventoryItem item1;
    		item1=new InventoryItem("hammer",20);
     
    		System.out.println("Item 1: ");
    		System.out.println ("Description: "+item1.getDescription());
     
    		//instaniate anothe object
     
    		InventoryItem item2;
    		item2=new InventoryItem(item1);
    		System.out.println("Item 1: ");
    		System.out.println ("Description: "+item2.getDescription());
     
    	}

    Over here, my teacher uses
    .getDescription
    My problem is that in the constructor of the first class I showed, why didn't he use
    .getDescription
    method instead of this
    .description
    May I know what is diference between these two. If I use .getdescription that would return the value of a field from that object too.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Passing Objects to Constuctors

    He is using a direct reference. The description variable is private, so a InventoryItem can directly reference a private variable of another InventoryItem. It's not my preferred way of doing it. I would have used the getDescription() method. Generally speaking, if you have a getter for a variable, you should usually use the getter instead of direct references. The only way he got away with a direct reference here is because the variable is still visible, even though it is private.
    Last edited by aussiemcgr; March 20th, 2014 at 03:13 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3

    Default Re: Passing Objects to Constuctors

    Glad to hear that since his method just confused me out! Thank you!

  4. #4

    Default Re: Passing Objects to Constuctors

    I take that back. I'm totally confused now, after seeing this writing of code being used in my book. In a hurry right and not so sure if I mentioned that
    .getDescription
    got used in a constructor.

    here is an another example I have in my book to clarify my issue. We have a class named Stock. The constructor for that is public
     
    private int symbol;
    private int sharePrice;
     
    public Stock(Stock object2)
    {
    symbol=object2.symbol;
    sharePrice=object2.sharePrice;
    }

    This is just a constructor that is being made for this class. Later on, I would also have a get_Symbol method that returns the value of symbol. Why can't I just use
    object2.get_symbol
    . Aussie if you still consider that to be the conventional way then I I'm will follow that approach, but if not may you explain what that code is doing.

    meanwhile, I just noticed that there is a number value on
    object2
    variable. I'f im correct, variable cannot have numbers in them...am I wrong here?

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Passing Objects to Constuctors

    Ok. You have to consider something called: scope.
    There are 4 keywords which indicate: Access Level (read: Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects))
    For our purposes, we are going to leave one out, and just say there are 3:
    1. public
    2. protected
    3. private
    These keywords go in front of instance variables, constructors, and methods to determine who can access the variables, constructors, or methods. To keep it simple, here are the basic rules:
    Public indicates it can be referenced by anyone. Protected indicates that it can only be referenced within its own class and by any subclasses. Private indicates that it can only be referenced within its own class (no subclasses even).

    So, let's say we have two classes:
    public class A {
    	private int var1;
    	protected int var2;
    	public int var3;
    }
    public class B extends A {
     
    }
    Now let's say we have a third class. This class will create a new instance of A and attempt to reference the variables. Read the comments to see how that works out:
    public class C {
    	public C() {
    		A obj = new A();
    		obj.var1; // NOT ALLOWED - var1 is private
    		obj.var2; // NOT ALLOWED - var2 is protected
    		obj.var3; // ALLOWED - var3 is public
    	}
    }
    Now let's expand the B class, doing the same sort of thing:
    public class B extends A {
    	public B() {
    		this.var1; // NOT ALLOWED - var1 is private
    		this.var2; // ALLOWED - var2 is protected, and B inherits from A
    		this.var3; // ALLOWED - var3 is public
    	}
    }
    Lastly, let's expand A to do the same thing:
    public class A {
    	private int var1;
    	protected int var2;
    	public int var3;
     
    	public A() {
    		this.var1; // ALLOWED - var1 is private, but we are in the A class
    		this.var2; // ALLOWED - var2 is protected, but we are in the A class
    		this.var3; // ALLOWED - var3 is public
    	}
    }

    Now, let's say class C really needed access to var1 or var2. How can we give it access without changing the access level of either variable? By creating getters:
    public class A {
    	private int var1;
    	protected int var2;
    	public int var3;
     
    	public int getVar1() {
    		return var1;
    	}
     
    	public int getVar2() {
    		return var2;
    	}
    }
    public class C {
    	public C() {
    		A obj = new A();
    		obj.var1; // NOT ALLOWED - var1 is private
    		obj.var2; // NOT ALLOWED - var2 is protected
    		obj.var3; // ALLOWED - var3 is public
    		obj.getVar1(); // ALLOWED - var1 is private, but getVar1() is public
    		obj.getVar2(); // ALLOWED - var2 is protected, but getVar2() is public
    	}
    }

    Do you understand all of that?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Passing objects between server & client
    By unSeenush in forum Java Networking
    Replies: 1
    Last Post: November 8th, 2012, 06:48 PM
  2. Replies: 1
    Last Post: October 19th, 2012, 08:18 AM
  3. passing java objects to ActiveMQ(JMS)
    By manju in forum Member Introductions
    Replies: 1
    Last Post: April 19th, 2012, 06:55 AM
  4. Objects passing to JSP
    By ober0330 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 03:19 PM
  5. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 05:31 AM