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

Thread: Create Objects on Demand?

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create Objects on Demand?

    //I am looking for the preferred way to create objects on demand, during
    //the execution of a program.
    //Example: I have a program that works with objects known as a Marble. I want to
    //create Marble objects with different names that I specify during the execution of the
    //program. It seems to me that this would require entering the name of the Marble from the
    //keyboard into a String variable, and then using that variable to call the creation
    //of a new Marble object. The problem is that the compiler balks about a duplicate
    //variable. It seems logical that objects should be able to be created as needed,
    //but how to do it?
    //
    //Below is an example program with a Marble class and a MarbleController class. The MarbleController is the main() entrance to the program.

    Thank you for any assistance in understand how this is done in Java.


    Here is my example code:

     
    public class Marble {
    public String size = null;
    public String color = null;
     
    //Default Constructor
    Marble(){
    size = "small";
    color = "blue";
    }
    }
     
    -------------------MarbleController.java
    import java.util.Scanner;
     
    public class MarbleController {
     
    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
     
    System.out.println("Enter a name for an Marble and press Enter");
    String marbleName = keyboard.nextLine();
     
    Marble marbleName = new Marble();
    //The compiler understandably balks about duplicate variable names when trying to use the variable "marbleName" as a placeholder for the entered name of a Marble object.
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Create Objects on Demand?

    You have to define the names of all variables at compile time.

    objects should be able to be created as needed,
    //but how to do it?
    You create new objects by using "new"

    Can you describe what you want the program to do and not what technique you want to use?
    There are probably ways to do the job besides the one you're trying to use.
    For example if you use a Map you can give a "name" to any object that is stored in the map.

  3. #3
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: Create Objects on Demand?

    Like Norm said, describe the program needs to do rather than the technique you want to use.

    A possible solution would be you have a loop and in the loop you create new objects and put it in a container like a array, list, map or even a custom container.

    Here is some Pseudo:
    int totalMarbles = 20; //amount of marbles > get from console?
    Hashtable bag = new Hashtable(totalMarbles);
    for(int i = 0; i < totalMarbles; i++)
    {
     bag.put(i, new Marble(Color.green)); //put 20 marbles in my bag
    }

  4. #4
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Create Objects on Demand?

    You have to define the names of all variables at compile time.

    Not quite true, but if a variable has no pointer or variable-name assigned to it, they will get garbage-collected. Assign pointers to the variables with a hashtable, and then set the keynames to the input from keyboard.

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

    Default Re: Create Objects on Demand?

    Do we have the same definition of a "variable"? A source symbol that refers to a memory location.
    I don't know of any way to refer to a location in memory without a variable.

  6. #6
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: Create Objects on Demand?

    Not saying thats not the case, but the thing he seems to be intrested in is deciding what the source symbol is supposed to be, which is impossible. However you can use another variable to set key names.


    Hashtable marbleBag = new Hashtable();
     
    marbleBag.put([I][B]theName[/B][/I], new Marble());

    this works, but defining mable *variable name* = new marble(); doesnt.

    And getting objects is easily done by

    (marble)marbleBag.get([I][B]theName[/B][/I]);

Similar Threads

  1. Help with background and objects
    By Afromiffo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2010, 01:19 AM
  2. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM
  3. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 08:01 PM
  4. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 09:39 PM
  5. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 06:29 PM