• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion about ArrayList<object> : ArrayList keeps A class's objects.  RSS feed

 
Srinesh Nisala
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two classes called 'A' and 'Main'.
First class A has field called name and a getter/setter of the field.

Second, class Main does kees objects of class A in his ArrayList called "list". In main method i have created a object of class A and class Main. and assigned value to name variable through a1 object. after that i added the a1 object to ArrayList.
And again i changed value of a1.name using setName() method. But i didn't assigned updated object to ArrayList. The thing is zero index of ArrayList should contain old name. but some how it has been updated, and i displays the updated name when i print it. i'm little bit confused how this happened. I kindly asking for your help on this matter. It would be nice if you can explain this with examples. Thank you!

Class A as follows



And class Main like bellow

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable 'a1' is not an A object, it is a reference to an A object. The ArrayList does not hold A objects, it holds references to A objects.

When you added 'a1' to the list the reference a1 was copied into the list. Both references point to the same object. This means that when you use 'a1' to change values in the object you will see the change if you use the reference in the list to look at the value.

Do you come from a C++ background? C++ allows objects to be stored as values on the stack, and would behave much like you were expecting here. Java only allows objects to be created on the heap, and variables are actually references.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one more thing don't use the name of the main to any other method.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RahulRise Das wrote: . . . don't use the name of the main to any other method.
Why not?
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code must be comprehensive. What if in time of whitebox testing ?
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain that more fully.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Again,

Sorry for replying late, was busy participating in some events,

Ok back to work,

What I'm Saying is if your testing several functions and some of them have the same name and type signatures like in this one wouldn't it be confusing to the human eye ? I know it sounds a bit weird but I like to keep things immaculate. I also know that the java run time system will detect the main method because it's static but still I Want to clarify two main methods are possible or not ?

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code in the original post has only one main method. It also has a class called Main, and a constructor for that class. All of these are distinct things.

There is nothing at all special about the main method, and the rules governing methods called main are exactly the same as for methods with any other name. The JVM expects the entry point to any Java application to have a particular signature, but its no more special than that.

A class can have multiple methods called main as long as they all have different signatures. Different classes can have methods called main with the same signature (including the signature that the JVM expects).
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your right Mike, I Misread the code.


Apologies Ritchie 'Sir'.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!