Results 1 to 6 of 6
  1. #1
    lockmac is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Unhappy object instantiation and arrayList

    Hi. I am reasonably new to Java but know the basics. I am just wandering if i could get some small help...

    I am creating an Enrolment System for a University (its my assignment) and I have a class called Unit, and a class called Enrolment. In the Enrolment class, I am writing a method for the user to create a new Unit, but I have the following problem, which might be hard to explain.

    Here is some of the code from the top...

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    public class Enrolment
    {
    	Scanner scanner = new Scanner(System.in);
    	private ArrayList<Unit> units;
    	
    	public Enrolment()
    	{
    		units = new ArrayList<Unit>();
    	}
    and here is the code from where I actually create the unit once I have the information from the user...

    Java Code:
    Unit 1005 = new Unit(name,code,sem1,sem2);
    units.add(1005);
    My question is... where it says '1005' (the name of the instance of Unit that I create)... is their a way so that it will change with every new unit? Perhaps so it will be the name of a variable, such as 'code'?? I tryed something like...

    Java Code:
    Unit code = new Unit(....)
    but obviously that just created it called 'code'.

    Just wandering what ways are their of making this change everytime a new instance is created.

    Thanks for your help

  2. #2
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    First correction in the code I like to make is we usually wont create objects of type ArrayList but only List
    Second I dont think 1005 is a valid name ....
    Ooooooooh enough of finding the mistakes Sorry dude :) :)

    About your question what I understood you need is the little bit complicated In fact I dont know the way to do that .. ie
    "the variable name will be dynamic "

    Actually do you really need something like that , From my three years of programming I was happy with the 'static' variables

    Cant you tell me a situation you will need this

  3. #3
    lockmac is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by goldhouse View Post
    First correction in the code I like to make is we usually wont create objects of type ArrayList but only List
    Second I dont think 1005 is a valid name ....
    Ooooooooh enough of finding the mistakes Sorry dude :) :)

    About your question what I understood you need is the little bit complicated In fact I dont know the way to do that .. ie
    "the variable name will be dynamic "

    Actually do you really need something like that , From my three years of programming I was happy with the 'static' variables

    Cant you tell me a situation you will need this
    Thanks for the reply mate. Well if I keep the name of the instance static (e.g. Unit101) and have that hardcoded into the program, wont everytime a new unit is created it will just point to the previous unit that was created (as they would have the same instance named of Unit101)? I don't really care at all what the name of instance is because it then goes into the ArrayList once created anyway from where I can just access it by index........

    OO yeh the name 1005 is just for illustration purpose... and also what do you mean about calling the object List? I thought that was the correct way to use the ArrayList...

    cheers
    Last edited by lockmac; 08-08-2007 at 02:26 AM.

  4. #4
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    Ok ArrayList in Java is an implementation of the interface List, Good programmers using the objects only for instantiation and use the interfaces for all the other purpose so you need not change the code at many places once you want to change the implentation.
    That is why I told you to use List instead of ArrayList

  5. #5
    lockmac is offline Member
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0

    Default

    Ok cheers for that I see where your coming from and have now fixed the problem.

    Have a good one guys
    Last edited by lockmac; 08-09-2007 at 10:09 AM.

  6. #6
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  2. Object ArrayList - increment solution needed badly!!
    By rugbyGeek in forum New To Java
    Replies: 4
    Last Post: 03-08-2008, 01:47 AM
  3. instantiation
    By Camden in forum New To Java
    Replies: 1
    Last Post: 03-02-2008, 05:44 PM
  4. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:57 PM

Posting Permissions

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