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.

Results 1 to 10 of 10

Thread: What language would describe this code so that I can research it?

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default What language would describe this code so that I can research it?

    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);

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What language would describe this code so that I can research it?

    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.

  3. The Following User Says Thank You to Norm For This Useful Post:

    FightingIrishman (January 14th, 2022)

  4. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: What language would describe this code so that I can research it?

    Thanks Norm.

    So, if I said that obj1 is an argument that's being sent to a constructor of obj2, would that be correct?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What language would describe this code so that I can research it?

    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.

  6. The Following User Says Thank You to Norm For This Useful Post:

    FightingIrishman (January 14th, 2022)

  7. #5
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: What language would describe this code so that I can research it?

    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?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What language would describe this code so that I can research it?

    passing an obj of a class as an argument to another class
    That happens all the time.
       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.

  9. The Following User Says Thank You to Norm For This Useful Post:

    FightingIrishman (January 14th, 2022)

  10. #7
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: What language would describe this code so that I can research it?

    Quote Originally Posted by FightingIrishman View Post
    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);
    Your wording makes me think you have not quite assimilated the difference between a class and a class object (instance). Better read up on the basics.
    And anyway that code is wrong - the class "Class" does not have a public constructor.

  11. #8
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What language would describe this code so that I can research it?

    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.)
    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 + "]"; 
        }
    }
    This is the output:
    Top left: [10, 12] 
    Bottom Right: [20, 24] 
    Rectangle: [10, 12] [20, 24]
    In the case above the class MyPoint has one constructor that takes two int arguments.
    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".
    MyClass a = new MyClass() 
    ...do something with a...
    MyClass b = new MyClass(a)
    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.

  12. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What language would describe this code so that I can research it?

    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.

  13. #10
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: What language would describe this code so that I can research it?

    Quote Originally Posted by Norm View Post
    The class name HelloWorld is not related to the code. A better name would be: TestConstructors
    Yes, that is the default class name from the online compiler.

Similar Threads

  1. What Program Do You Use To Code For Java Language?
    By RM3 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2018, 05:57 PM
  2. Please describe the output of the following program!
    By sgt98 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 15th, 2014, 12:31 PM
  3. Please describe the output of the following program!
    By sgt98 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 15th, 2014, 09:21 AM
  4. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:03 PM
  5. Replies: 5
    Last Post: September 6th, 2009, 04:39 AM