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

Thread: Creating objects from methods

  1. #1

    Default Creating objects from methods

    public class demo
    { 
    Public class static void main(String[]args)
    {
    //Creating a variable that will be a reference to the object
    Peoples person_one;
     
    //create an object using void method
     
    Create_object(person_one);
    }
     
    Public static void Create_object(Peoples object)
    {
    Object=new Peoples();
    }

    I have assembled this code below that has a void method which will creat a new object. Problem I encounter is that in
    Create_object(person_one);
    the person_one has an error saying not initialized. I'm jus trying to learn on my own ways here and practice so may know what's wrong with this? I know I can use a return object from methods but what about this approach?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Creating objects from methods

    I'm not fully sure about your question as you did not post the error (and your code is difficult to read due to a lack of convensions, loss in formatting, and not compileable), but here are two references I encourage you to read:
    Code convensions: Code Conventions for the Java Programming Language
    Pass by value: Does Java pass by reference or pass by value? | JavaWorld

  3. #3

    Default Re: Creating objects from methods

    Thank your for your response! I'm aware that's not the conventional way of writing a code; however, I was using my ipad during that time. Apologize for my mistake. Also, I did peak on to the links your provided and they were helpful. Meanwhile for my code, as I have fixed it
     
    public class demo
     
    { 
    Public class static void main(String[]args)
    {
    //Creating a variable that will be a reference to the object
    Peoples person_one;
     
    //create an object using public void method. This line of code will call the create_object method and make a object referenced to person_one
    create_object(person_one);
    }
     
    //This function will take in an argument of type People(a People Class) and create an object
    public static void Create_object(Peoples object)
    {
    object=new Peoples();
    }

    I'm getting my error on
    create_object(person_one);
    person_one is where I'm getting an error on. All my error says is that
    HTML Code:
    local variable person_one may not have been initialized
    . I hope this did help! Thank you in advance for taking your time to help me!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Creating objects from methods

    The error message is in good English. Do you understand what it says?

    Do some research on pass parameters by value. Java uses pass by value when passing parameters to a method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5

    Default Re: Creating objects from methods

    I do understand what the error is. Basically the variable is not initialized to any value. From my understanding and from what I have read, when passing objects to methods, the reference variable is passed instead of value. I'm just having trouble understanding why I have an initialization error. I know if I make a method that returns an object this will works but why not here?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Creating objects from methods

    why I have an initialization error.
    Because there is not a value assigned to the variable BEFORE it is used.

    the reference variable is passed instead of value.
    No. The value of the variable is passed, not a reference to the variable.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7

    Default Re: Creating objects from methods

    Ok, so I'm reading my java book and this is what it reads
    when an object is passed as an argument, it is actually a reference to the object that is passed
    . The book also mentions that
    when a variable is passed as an argument to a method, it is said to be passed by value.
    Either I'm interpreting this wrong, which I believe I am, or I doubt your actually wrong since your more expierenced

    Now suppose you are correct about this, then in my book there is this following code
    public class PassObject
    {
    public static void main(String[]args)
    {
    //Create an inventoryItem object
    InventoryItem item=new InventoryIem("Wrench",20);
     
    //Pass the object to displayItem method.
    displayItem(item);
    }
    pblic static void displayItem(InventoryItem I)
    {
    System.out.println(i.getDescription());
    }
    }

    Like you said that it is passed by value, then I visually can't see what value is being passed on to my method that has the object argument in it.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Creating objects from methods

    The value of a variable that holds a reference to an object is passed to a method. The variable that holds that reference is not accessible in the method that the value is passed to and can't be changed by the method. The contents of the object can be changed (if the fields and methods allow it) by any code that has a reference to the object.

    BTW The code you are posting has lost its indentations which makes it hard to read and understand. Please add proper indentations to the code you post.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Help with objects, methods, and class inheritance.
    By Gingerbread Fetus in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2013, 09:02 AM
  2. [SOLVED] Creating an array of objects
    By Benner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 22nd, 2013, 03:00 AM
  3. Creating random objects
    By JackCannon15 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 18th, 2011, 07:50 AM
  4. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM
  5. [SOLVED] Word Scramble Using Objects & Methods
    By Whitechapel in forum Object Oriented Programming
    Replies: 3
    Last Post: May 23rd, 2010, 12:38 PM