Reputation: 87
public class HelloStack{
private String myName;
private int myAge;
private String myHeritage;
HelloStack(String myName, int myAge, String myHeritage){
this.myName = myName;
this.myAge = myAge;
this.myHeritage = myHeritage;
}
Then in my main()
I create an object:
HelloStack hiGuys = new HelloStack("joseph", 89, "indian");
If my understanding is correct on the role of parameters here, I'm passing the values from hiGuys
into the constructor Hellostack
, and then they're actually initialized in the constructor when the compiler runs through?
Upvotes: 0
Views: 25
Reputation: 11740
No. First, you create the HelloStack object:
new HelloStack("joseph", 89, "indian");
Then a few things happen with object loading and instantiation that aren't relevant. The next thing is that the thread enters the constructor, calls an implicit super()
constructor, and then sets the fields one at a time:
this.myName = myName;
this.myAge = myAge;
this.myHeritage = myHeritage;
Then the constructor "returns" the new object. That object is assigned to your hiGuys
variable.
HelloStack hiGuys =
So now hiGuys has a single value - that new object.
Upvotes: 1