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: Using this reference variable on constructors

  1. #1

    Default Using this reference variable on constructors

    I’m trying to simplify my constructor by using “this” reference variable. I’m aware how to use this reference when making constructors that involve fields of primitive data types; however, not so sure fields involving objects. I have made the following constructors and haver placed a question mark in the last parameter. Can anyone be willing to tell me and explain to me how I can use this reference that involves objects. Also, this chapter im learning related to inner classes.
     public class RetailItem
    {
                    //Create the fiels for item.
                    //You need item name and item number. then you would need
                    //cost
                    private String description;
                    private int Item_Number;
                    private CostData cost;
     
                    //Retail Item construcotr
                    public RetailItem()
                    {
                                    this("",0,0,0,?);
                    }
     
                    //Constructor 2
                    public RetailItem(String description)
                    {
                                    this(description,0,0,0,?);
                    }
     
                    //constructor 3
                    public RetailItem(String description,int Item_Number)
                    {
                                    this(description,Item_Number,0,0,?);
                    }
     
                    //constructor 4
                    public RetailItem(String description,int Item_number,double Retail,double wholesale, CostData object)
                    {
                                    this.description=description;
                                    this.Item_Number=Item_Number;
                                    cost=new CostData(Retail,wholesale);
                    }

    Here is partially done inner class
     private class CostData
                    {
                                    //Fields of this
                                    private double wholesale_Price;
                                    private double Retail_price;
     
                                    //Create a constructor
                                    public CostData(double wholesale_Price,double Retail_price)
                                    {
                                                    this.wholesale_Price=wholesale_Price;
                                                    this.Retail_price=Retail_price;
                                    }
     
                                    public String toString()
                                    {
                                                    DecimalFormat formatter=new DecimalFormat("#0.00");
     
                                                    String data="Price Information\n"+"WholeSale Price: "+formatter.format(wholesale_Price)+"\nRetail Price: " +formatter.format(Retail_price);
                                                    return data;
                                    }
                    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Using this reference variable on constructors

    Why even have the CostData object in constructor 4? You aren't using the variable; you are creating a new one from the Retail and wholesale variables.
    Regardless, since you are not using that variable, you could just pass null.
    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: Using this reference variable on constructors

    In my book, it had the code
    public RetailItem(String desc,int itemNumber,
    double wholesale, double retail)
    {
     
    description=desc;
    ItemNumber=itemNumber;
    cost=new CostData(wholesale, retail);
     
    }

    Being aware I could have use this method provided by the book, but for future reference, suppose theirs were objects declared in the fields. That's why I wanted to use this method and see how it works. Now, asking you this, what would be a conventional way to do this?

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Using this reference variable on constructors

    Exactly. Look at constructor #4, and compare it to your book example.
    Instead of:
    public RetailItem(String description,int Item_number,double Retail,double wholesale, CostData object)
    You want:
    public RetailItem(String description,int Item_number,double Retail,double wholesale)

    Then you don't even need to worry about that last parameter, since you aren't using it.

    Or, is your intention to use the object parameter instead of creating a new CostData object in the constructor?
    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/

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Using this reference variable on constructors

    Quote Originally Posted by fahman_khan75@yahoo.com View Post
    I’m trying to simplify my constructor by using “this” reference variable. I’m aware how to use this reference when making constructors that involve fields of primitive data types; however, not so sure fields involving objects.
    I'm not entirely sure if I completely understand your post, but it appears there is some confusion with the "this" keyword. The "this" keyword has 2 uses: (1) to refer to a field in the class, and (2) to call another constructor in the same class. See Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects).

    In the code in post #1,

    Quote Originally Posted by fahman_khan75@yahoo.com View Post
                    public RetailItem()
                    {
                                    this("",0,0,0,?);
                    }
    "this" (not a reference variable) is used to call the 5-parameter version of the constructor, which is
    public RetailItem(String description, int Item_number, double Retail, double wholesale, CostData object)

    Since the 5th parameter is of type CostData, all you need to do is to create an instance of CostData, and pass the reference to the instance of CostData in place of '?'. However, since the "this" constructor call must be the first statement in a constructor, you'll need to do the CostData instantiation inline, e.g.,

    this("", 0, 0, 0, new CostData(0, 0));

    The above will not work with your current implementation of the CostData inner class because the inner class (CostData) has an implicit reference to an instance of the outer class (RetailItem). The inner class can be instantiated only after the outer class is instantiated unless the inner class is static. Therefore to do the above you'll need to change CostData so that it looks like the following:

    private static class CostData

    An alternative will be to move CostData out of RetailItem so that CostData is no longer an inner class, and instead becomes a "regular" class. This way you don't need to make CostData static.

    Finally, in the 5-parameter constructor since an instance of CostData is being passed in via the "object" parameter, you only need to assign it to the cost field, i.e.,

    	public RetailItem(String description, int Item_number, double Retail, double wholesale,
    		CostData object) {
    		this.description = description;
    		this.Item_Number = Item_Number;
    		this.cost = object;
    	}

    Btw, the above excerpt has a small error. "this.Item_Number = Item_Number;" has no effect because Item_Number (with uppercase 'N') is the field name, whereas the constructor's 2nd parameter name is Item_number (with a lowercase 'n'). I would highly recommend that you follow the Java code convention. See Code Conventions for the Java Programming Language.

  6. The Following User Says Thank You to jashburn For This Useful Post:

    GregBrannon (April 2nd, 2014)

Similar Threads

  1. a superclass variable can reference a subclass but here why cant cv access ???
    By prajwalfc in forum Object Oriented Programming
    Replies: 1
    Last Post: June 20th, 2012, 07:16 AM
  2. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 01:30 AM
  3. JButton Auto-changing Reference Variable
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 19th, 2011, 10:57 PM
  4. cannot find symbol in reference variable
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 08:21 PM
  5. Fitting a large primitive into a small reference variable
    By Phobia in forum Java Theory & Questions
    Replies: 15
    Last Post: October 23rd, 2009, 03:10 PM