I'm wondering what is happening to the creation of the class named "obj2" when another object named "obj1" inside the parenthesis. What is the technical term or the language used to describe what's happening here?
Class obj2 = new Class(obj1);
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I'm wondering what is happening to the creation of the class named "obj2" when another object named "obj1" inside the parenthesis. What is the technical term or the language used to describe what's happening here?
Class obj2 = new Class(obj1);
obj2 is a variable that refers to an instance of the Class class.
obj1 is a variable; The datatype is not shown. The datatype is set when a variable is declared.
If you don't understand my answer, don't ignore it, ask a question.
FightingIrishman (January 14th, 2022)
Thanks Norm.
So, if I said that obj1 is an argument that's being sent to a constructor of obj2, would that be correct?
Not quite. obj1 is an argument being passed to the constructor of the Class class. obj2 is the reference to the instance of Class created by the constructor.
If you don't understand my answer, don't ignore it, ask a question.
FightingIrishman (January 14th, 2022)
That's hard to wrap the head around. Can you point me in the direction of any material to explain passing an obj of a class as an argument to another class?
That happens all the time.passing an obj of a class as an argument to another class
System.out.println("a String"); // Pass String object to println method of PrintStream class ... Scanner scnr = new Scanner(new File("aFile.txt"));// pass File object to Scanner class's constructor
If you don't understand my answer, don't ignore it, ask a question.
FightingIrishman (January 14th, 2022)
The example you have shown is a class instance being declared and an instance of another class as an argument to the constructor.
Every class has a constructor. If the coder doesn't provide one the compiler will use a default constructor with no arguments.
A class can have more than one constructor as long each is unique in its argument declaration (the parameters).
If you provide a constructor/s all instances of that class must be declared using one of the constructors you provided.
Here is an example of passing the instance of a class to the constructor of another class.
(You can copy paste this code to this online compiler and run it.)
This is the output:public class HelloWorld{ public static void main(String []args){ MyPoint topLeft = new MyPoint(10,12); // create a MyPoint instance System.out.printf("Top left: %s \n", topLeft.toString()); MyPoint bottomRight = new MyPoint(20, 24); // create another MyPoint instance System.out.printf("Bottom Right: %s \n", bottomRight.toString()); MyRect rect = new MyRect(topLeft, bottomRight); // create a MyRect instance System.out.printf("Rectangle: %s \n", rect.toString()); } } class MyPoint{ private int _x; private int _y; public MyPoint(int x, int y){ // this is the constructor for MyPoint - it takes two int arguments this._x = x; this._y = y; } public int getX(){ return this._x; } public int getY(){ return this._y; } public void setXY(int x, int y){ this._x = x; this._y = y; } public String toString(){ return "[" + this._x + ", " + this._y + "]"; } } class MyRect{ private int _x1; private int _x2; private int _y1; private int _y2; public MyRect(MyPoint p1, MyPoint p2){ // this is the constructor for MyRect - it takes two MyPoint arguments this._x1 = p1.getX(); // the values from MyPoint arguments p1 and p2 are copied to local private vars. this._y1 = p1.getY(); this._x2 = p2.getX(); this._y2 = p2.getY(); } public int getX1(){ return this._x1; } public int getY1(){ return this._y1; } public int getX2(){ return this._x2; } public int getY2(){ return this._y2; } public MyPoint getTopLeft(){ return new MyPoint(this._x1, this._y1); } public MyPoint getBottomRight(){ return new MyPoint(this._x2, this._y2); } public String toString(){ return "[" + this._x1 + ", " + this._y1 + "] [" + this._x2 + ", " + this._y2 + "]"; } }
In the case above the class MyPoint has one constructor that takes two int arguments.Top left: [10, 12] Bottom Right: [20, 24] Rectangle: [10, 12] [20, 24]
The compiler will give you an error if you try to initialize a MyPoint variable without providing exactly 2 int arguments.
You can even declare a constructor to accept an instance of its own class as the argument. This is done to create a "Copy Constructor".
More info about it here although it gets a little overwhelming, take in what you can and come back to it when you have a better understanding of how class instances and object references work.MyClass a = new MyClass() ...do something with a... MyClass b = new MyClass(a)
Thanks for the sample code.
One small comment: The class name HelloWorld is not related to the code. A better name would be: TestConstructors
Also Many of the comments are redundant.
If you don't understand my answer, don't ignore it, ask a question.