Results 1 to 7 of 7
  1. #1
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Help in object instance creation

    Good morning all,
    So I am not sure why i am having a mind blank at the moment but for some reason I am.

    I am trying to create an application for a school project that requires me to show how I use classes and methods. So this part is not required, just something I want to add.....
    The project is dealing with creating our own date class (not using built in date classes)
    I was thinking that I would have the user enter the date, then let them enter another date, and another date but where I am allowing myself to be stumped is how to have each entry create an instance of a class when the user enters a new date. The menu will have them press q when they are done and then I will show them information about the dates they entered.

    I have a class called MyDateClass
    I know in the main I can create like this:

    MyDateClass dateOne = new MyDateClass();
    MyDateClass dateTwo = MyDateClass();

    but for the life of me (maybe because it is Sunday morning) I can't figure out how to make that dynamic

    In case the user enters multiple dates, their first entry would be

    MyDateClass date(A counter added here maybe) = new MyDateClass();

    The user could enter 10 dates if they chose, or 1 date or 100 so I guess I am just trying to get past how to create a dyanmic instance of a class
    I am not asking for full blown solutions, just guidance to get my direction back on track.

    Thanks...
    Wally

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help in object instance creation

    I think that you may be trying to put too much stuff into your Date class. If this is to be a well-behaved class, then all Date concerns itself with is storing and showing date data, that's it. It likely will have fields for month, year, and day of month. It will likely have a constructor or constructors, getter and setter methods. What it should not have is any code that interacts with the user or any code concerning itself with more than one date since this code belongs in other classes that *use* your Date class.

    You should work on create a Date or MyDate class first that is simple and clean. You should work on a TestMyDate class that has a main method and nothing else where you interact with the user and create and display a MyDate object or two. Then later if you want to work with multiple MyDates or collections of MyDate objects, create a separate class for that.

  3. #3
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Help in object instance creation

    the MyDateClass does not have any interaction with the user, In my main class Project1Class I have the user interaction that calls the MyDateClass to perform tests on the dates and validates the input etc... So my current layout has a Class where main runs.... and calls on MyDateClass which does in fact have constructors, get and set methods etc.... I am sorry for not making it clear that I have the Project1Class that I am using to call the dateClass, I guess my question should have been.... from my Main Project1Class file, how can I get it to dynamically create instances of MyDateClass. I think in writing this I am getting some clarity, but for the life of me I am allowing myself to get stuck.

  4. #4
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Help in object instance creation

    And i would post my code, but out of my frustration, I deleted and am starting over. OK the deletion was not intentional, but through my frustration I did it. I will post my code once I redo it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help in object instance creation

    This line:

    Java Code:
    MyDateClass date(A counter added here maybe) = new MyDateClass();
    Suggests that you want to use either an array of MyDateClass or an ArrayList of MyDateClass.

  6. #6
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Help in object instance creation

    That code was just my thought process as to how to potentially create a dynamic class. I don't think I would want to use an array as that has finite entries, but having said that, maybe a static variable that is incremented each time an instance is created could be used in naming the creation of the next instance and then incremented again. Still haven't fully cleared it up in my head, but I love these forums because the generate thought that most often leads to a solution.

    Wally

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help in object instance creation

    Quote Originally Posted by wfsteadman View Post
    That code was just my thought process as to how to potentially create a dynamic class. I don't think I would want to use an array as that has finite entries, but having said that, maybe a static variable that is incremented each time an instance is created could be used in naming the creation of the next instance and then incremented again. Still haven't fully cleared it up in my head, but I love these forums because the generate thought that most often leads to a solution.
    In the grand scheme of things, variable names are not that important or that flexible, and don't really exist (for the most part) in compiled code. You cannot really create a variable name from a String and an int variable even if you wanted to.

    Instead please consider my other suggestions:
    • Use an array of MyDateClass objects. Yes this has a fixed size, but you could create an array with a size that is "big enough" to hold enough MyDateClass objects, and then use a maxSize int variable to hold the actual number of MyDateClass instances that it holds. Or,...
    • Again use an ArrayList of MyDateClass objects. If you're using generics, this would be declared as an ArrayList<MyDateClass>. I'm not sure if you've used these before but they function as if they were variable sized arrays, just what it sounds like you may be looking for.
    • Other collection options exist, including a Map such as a HashMap<String, MyDateClass> where you store MyDateClass objects in this collection and associate them with a unique String. Think of this as functioning similar to an array or ArrayList, but rather than using a number index to retrieve the object of desire, you'd use a String (or other types can be used for the key if desired).

Similar Threads

  1. Object Creation
    By Everyman in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 03:36 PM
  2. need help on object creation
    By krishanu in forum New To Java
    Replies: 3
    Last Post: 05-28-2011, 04:56 PM
  3. object creation
    By enygma in forum Java Applets
    Replies: 0
    Last Post: 01-05-2010, 10:01 AM
  4. Object creation and construstion
    By abimaran in forum New To Java
    Replies: 8
    Last Post: 12-15-2009, 09:58 AM
  5. Accessing instance outside of creation method
    By meringue in forum New To Java
    Replies: 6
    Last Post: 03-22-2009, 01:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •