0

Please correct me if I am wrong, I just want some clarification that I'm understanding this right.

When you create an object in java you use the new keyword followed by the class type. Ex. new [someclassnamehere](); Depending on your constructors you can pass arguments by supplying them in the parameter when you create the object.

I'm not sure if it would ever be useful to just have a line of code that creates a new object o by just using the new keyword, because nothing is actually holding the reference to that information. But it's correct it seems.

So you can create a variable that contains the reference to the new object you are creating by using type name.Such as: Employee someData; . But it has yet to actually reference an object, seeing as one hasn't been created yet. So by applying the above information discussed: Employee someData = new Employee(name); We now created an Employee object that contains some name of the employee. The new keyword created an instance of the class Employee, an object, in which the someData variable references that newly created object.

So now the someData variable can be said to reference the Employee object because it contains the address in memory of where the object is stored. This address will allow us to access the actual data of the object, in this case the name of the employee.

If I were to create an ArrayList that has the datatype Employee, I can store Employee objects in it. So I can add the someData variable to the arrayList as well as someData2,someData3,etc. (Just assuming they are all of the same type but contain different employee information). So each of those variables contain references to these objects. The ArrayList object then contains references to these objects as well because the ArrayList elements contain these someData variables which reference the Employee Objects.

example:

ArrayList.get(1) -> someData ->  reference variable(address) -> employee Object
ArrayList.get(2) -> someData2 ->  reference variable(address) -> employee Object2

Pretty sure I got the idea down so far, but what slightly throws me off is when you actually don't create a reference variable.

So let's say you create a loop which creates an object(data is being read from some database,etc) and adds it to an ArrayList. In this loop you collect the data you want and use that to create an object, which is then directly added to an ArrayList.

Let's say it looks like this (combination of some pseudo code and actual code)

ArrayList<Employee> list = new ArrayList<Employee>();
// While data from the database still exist (while loop,etc)
// extract some sort of data from the database, such as their name and hours
// create an object of this information and store it in an ArrayList
list.add(new Employee(name, hours);
// end loop

For simplicity, let's said the loop ran 5 times so it created 5 objects. This means it added 5 objects to the ArrayList List and the references to these objects are actually contained in the ArrayList elements. So to get the first object added to the ArrayList you would use list.get(1) (I'm pretty sure ArrayList start at 1 and not 0 for indexes), which returns the reference to that object.

EDIT: Please forgive me for my mistake here, I suppose I had a brainfart and got mixed up. I don't know why I thought ArrayList indexes worked like that for a second

Is this correct and standard way of creating objects through the use of a loop?

pudge
  • 29
  • 1
  • 1
  • 5
  • The answer is "YES," but I'm not actually sure what you want to know. This question is extremely broad. – 4castle Apr 15 '16 at 00:36
  • Everything except for _I'm pretty sure ArrayList start at 1 and not 0 for indexes_ is correct. A _reference variable_ is a variable of some reference type. `Employee` is a reference type. So `Employee someData;` is a declaration of a reference variable. – Savior Apr 15 '16 at 00:36
  • *"the ArrayList elements contain these someData variables"* This is where I'm not sure that you have it right. The ArrayList doesn't have variables in it, it also has references. When you call `theArrayList.add(someData)`, `someData`'s value (the address of the object it points to) is copied. What you end up with are two separate references (one in the `someData` variable, one in the `ArrayList`) which point to the same object. Java references are like pointers. Possibly see http://stackoverflow.com/q/40480/2891664. – Radiodef Apr 15 '16 at 00:46
  • @Radiodef Ahh! You are totally right, I forgot that it actually copies the reference variable to the element at some n index. I got all this to work properly, it just felt a little weird, as I never created objects through a loop before. I guess I just wanted to talk it out the explanation to myself with some outside confirmation. – pudge Apr 15 '16 at 00:53

1 Answers1

0

I'm not sure if it would ever be useful to just have a line of code that creates a new object o by just using the new keyword, because nothing is actually holding the reference to that information. But it's correct it seems.

Imagine that the constructor starts a new thread. No reference, but

new AmazingThread();

is fine in that case.

Well, this question is huge. And yes, creating objects like this is completely legit. You can think of ArrayList having it's own variable storing the reference.

  • Thanks for the input. Yeah It seems to make sense now that I've looked over it, read the comments here,etc. It just felt a little off because I never really had to approach a problem like this before, hence why I wanted some clarification on my thinking. Have a good day/night! – pudge Apr 15 '16 at 02:37