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 15 of 15

Thread: I need to retreive data from my constructor that does not have its own variable

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default I need to retreive data from my constructor that does not have its own variable

    I created a new object, and it is a parameter for a new constuctor. I can't define a value in the new constructors class for it to have a spot to save the information to, and for me to thus easily access it.

    so for example...

    private int x;
    private int y;

    public MyConstructor( int a, int b, newObject c )
    {

    x = a;
    y = b;


    }

    public newObject getNewObject()
    {
    ? help ?
    }

    any ideas?


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: I need to retreive data from my constructor that does not have its own variable

    What specifically are you trying to accomplish?

    Format your code to preserve formatting.

    [C0DE=java]
    <code>
    [/CODE]

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: I need to retreive data from my constructor that does not have its own variable

    I want to get the input parameter ( my object which HAS A relationship ) by calling a get method.

    It's an assignment.

    I realize it would be incredibly easy to just define some variable... private newObject a; and then assign it in the constructor, but I can't do that. My teacher only defined those two variables in the UML.

    So... There must be some way to do it.

    --- Update ---

    Additionally, the getNewObject() is in the UML as well... just have no idea how the hell to access the data, when it's not saved to a variable somewhere... except in the main method where the instance is created... sigh...

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: I need to retreive data from my constructor that does not have its own variable

    help

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need to retreive data from my constructor that does not have its own variable

    I want to get the input parameter
    what is the name of the variable that is the input parameter?

    Is NewObject the class of an object that is to be returned by the getNewObject() method?
    Where is the definition/declaration of the NewObject object that is to be returned? Was the constructor supposed to create an instance of NewObject and save it in a class variable?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: I need to retreive data from my constructor that does not have its own variable

    Okay. Check it. I want the getMyObject() method to work without adding any new fields. I can do anything as long as it doesn't implement a new field.

     
    public class MyObject
    {   
        private int x, y;
     
        public MyObject( int xValue, int yValue )
        {
            x = xValue;
            y = yValue;
        }
     
        public int getX()
        {
            return x;
        }
        public void setX( int xValue )
        {
            x = xValue;
        }
        public int getY()
        {
            return y;
        }
        public void setY( int yValue )
        {
            y = yValue;
        }        
    }
     
    public class MyComposition
    {
        private String a, b, c;
        public MyComposition( String aValue, String bValue, String cValue, MyObject aValue )
        {
            a = aValue;
            b = bValue;
            c = cValue;
        }
     
        public MyObject getMyObject()
        {
            return aValue;
        }
    }
     
    public class MyCompositionApp
    {
        public static main( String[] args )
        {
            MyObject object = new MyObject( 5, 15 );
            MyComposition composition = new MyComposition( "This", "Is", "My", object );
     
            System.out.println( composition.getMyObject() );
     
        }    
    }
    [/highlight]

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need to retreive data from my constructor that does not have its own variable

    want the getMyObject() method to work
    Define what the method is supposed to return and where it is going to get it.
    If it's to return an object, can it create a new object every time it is called?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: I need to retreive data from my constructor that does not have its own variable

    where it is going to get it.
    This is my problem. I want the getMethod to return the overrided toString() of MyObject.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need to retreive data from my constructor that does not have its own variable

    Where is the instance of the MyObject class that can be used to call its toString() method?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: I need to retreive data from my constructor that does not have its own variable

    Error

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need to retreive data from my constructor that does not have its own variable

    Where is the instance of the MyObject class that can be used to call its toString() method?

    I don't see where the MyObject class used in the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: I need to retreive data from my constructor that does not have its own variable

    MyObject is a parameter of MyComposition. I didn't write the toString() method in this mock code, but it would look something like

    public String toString
    {
    return String.format( "%d, %d", x, y );
    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need to retreive data from my constructor that does not have its own variable

    What have you tried? What happened?

    It looks like the posted code has changed some. Is there anything worth looking at now?
    Please explain which code has the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Jan 2013
    Posts
    22
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: I need to retreive data from my constructor that does not have its own variable

    Basically nothing, except googling for hours with no result. I have no idea how to access the object without using inheritance, or setting another variable like private MyObject x; and then defining it in the constructor.

    Maybe there's a way to access it through the package? All three are in the same package.

    The new code just shows further clarification about how the code would be used. Nothing is different except that I added a main method with instances of the classes.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need to retreive data from my constructor that does not have its own variable

    how to access the object
    What object? Is there a variable name that holds a reference to the object?
    Where does an instance of the object exist?
    If there isn't an instance, can you create one?

    What is supposed to be returned by the method? Where does the data that is supposed to be returned exist?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to get a data from database and store it in a variable?
    By jabaman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2012, 03:34 PM
  2. Replies: 0
    Last Post: October 30th, 2012, 10:06 AM
  3. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  4. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM