Learn Java Tutorial for Beginners, Part 18: Constructors

///Learn Java Tutorial for Beginners, Part 18: Constructors

Learn Java Tutorial for Beginners, Part 18: Constructors

FavoriteLoadingAdd to favorites

Get complete courses at
A tutorial on constructors in Java; what constructors are and how to use them, how to create multiple constructors and how to call constructors within other constructors.

source

Java468x60
By |2019-06-10T19:32:10+00:00June 10th, 2019|Java Video Tutorials|40 Comments

40 Comments

  1. Yuchen Wang June 10, 2019 at 7:32 pm - Reply

    Thanks John! Wonderful job!

  2. Ziqiang Cheng June 10, 2019 at 7:32 pm - Reply

    easily the best java lesson, watching it at 2018 and made my days breeze

  3. Andrew Howe June 10, 2019 at 7:32 pm - Reply

    easily the best java tutorials on this site. Thanks

  4. parker June 10, 2019 at 7:32 pm - Reply

    there no way that your real voice lmao

  5. Aziz Bassim June 10, 2019 at 7:32 pm - Reply

    perfect videos…very helpful except the constructors by itself are complicated way to do simple things

  6. Cameron Bradshaw June 10, 2019 at 7:32 pm - Reply

    you should teach at the University

  7. Bert86 June 10, 2019 at 7:32 pm - Reply

    why do you need to create a class variable called name when you can just pass it straight into the constructor ?

  8. Aq June 10, 2019 at 7:32 pm - Reply

    I have watched many Java tutorials on YouTube, and to be honest, yours are the best! Thank you!

  9. Guram Kankava June 10, 2019 at 7:32 pm - Reply

    Thank you very much

  10. Shivam Pandey June 10, 2019 at 7:32 pm - Reply

    Sir, You said that calling a constructor from another one should be in the first line, does this mean I can only call one constructor?
    for example: If I have made three constructors just like you did but with different parameters, Can I only call one Constructor? What if I wanna call two constructors from the one that I'm calling in the main?

  11. Heat4Life June 10, 2019 at 7:32 pm - Reply

    Thank you so much, I feel so smart at School because of this Haha 🙂

  12. Phi Φ June 10, 2019 at 7:32 pm - Reply

    I understand somewhat. I don't think I'm intelligent enough to grasp certain Java concepts. My thoughts gets all mixed up while trying to concentrate.

  13. Jeff R June 10, 2019 at 7:32 pm - Reply

    I finally understand constructors. Thank you.

  14. shl sid June 10, 2019 at 7:32 pm - Reply

    Why not just assign the value of a variable when declaring it globally rather than in a constructor?

  15. Bad Seal Studios June 10, 2019 at 7:32 pm - Reply

    I'm following your vids and it helps me with my cs class! (sometimes I'm ahead but I don't want to get too far ahead). Keep uploading vids thanks.

  16. Artem Lobov June 10, 2019 at 7:32 pm - Reply

    Whenever I run the first piece of code, it says that class Machine is already defined and won't compile.

  17. Taurus Gemini June 10, 2019 at 7:32 pm - Reply

    I have to call you Sir because you are that good at teaching; hats of to you sir

  18. YOUSSEF HL June 10, 2019 at 7:32 pm - Reply

    where can i find poo
    don't tell me to google that cause i want it from cage of programing

  19. Massi June 10, 2019 at 7:32 pm - Reply

    The constructor creates the object that DOES the method:I make a Point like that:
    class Point
    {public Point( int x,int y)
      {this.x=x; this.y =y;}
    //then the method will do something with my objet
    pubic void showCoord()
    { system.out.println("coord  are "+ x+ ", "+y}
    private int x,y;
    }

  20. Lady Foxy June 10, 2019 at 7:32 pm - Reply

    Very helpful video. However, what about setting the hours and minutes in a Timer () constructor? (yes, I'm recreating the old C++ Timer program for practice) The hours and minutes are of type Display. How would I set hours = 24 and minutes = 60 in the constructor if they are a non-primitive data type?

  21. Not Legato June 10, 2019 at 7:32 pm - Reply

    this is very useful, but i still can't help but wonder why i'd ever use a constructor instead of simply making normal methods instead.

  22. Guillermo Rendon June 10, 2019 at 7:32 pm - Reply

    I gather also that a constructor is simply a method with the same name as the class it belongs to.

  23. Guillermo Rendon June 10, 2019 at 7:32 pm - Reply

    Still don't understand the advantage of creating a constructor vs. creating a method.

  24. Guillermo Rendon June 10, 2019 at 7:32 pm - Reply

    Can you ask the constructor method to run any type of commands? Or is it reserved for special type of processes? Hope my question makes sense….

  25. Guillermo Rendon June 10, 2019 at 7:32 pm - Reply

    Brilliant teaching on the "this" command. Learning more about constructors and the "this" command. #1

  26. M Ally June 10, 2019 at 7:32 pm - Reply

    I did something like this .. can you check 

    import java.util.Scanner;

    public class ConstructorExample {

    public static void main(String args[]){

    Thing thing1, thing2;
    thing1 = new Thing();
    thing2 = new Thing();
    }

    }

    class Thing {

    private String name;
    private int qty=0, code=0;
    static int count=0;

    public Thing(){
    ++count;
    code=this.count;
    new Thing(qty,code);

    }
    public Thing(int qty ,int code){

    Scanner scan=new Scanner(System.in);
    System.out.println("Enter the Qty of the Code: "+code);
    qty= scan.nextInt();
    new Thing(name, qty, code);
    }

    public Thing(String name, int qty, int code){
    Scanner scan=new Scanner(System.in);
    System.out.println("Enter name of Thing: ");
    name=scan.nextLine();
    System.out.println("Code: "+code+" Name of Thing: "+name+" Quantity is :"+qty);

    }

  27. M Ally June 10, 2019 at 7:32 pm - Reply

    can i call another Constructor from the first Constructor? 
    new Thing(String name, int code);

    "this" will work with static only?

  28. David Borchgrevink June 10, 2019 at 7:32 pm - Reply

    really helpful!  thanks for your time!

  29. luis sanchez villegas June 10, 2019 at 7:32 pm - Reply

    hey i dont find any use of the constructors because i try but i think that the methods can do that either , why will i set an object to specific variables if i have the set methods can someone help me

  30. Jake Shi June 10, 2019 at 7:32 pm - Reply

    Thank you. Great job. For the second constructor Machine(String name), is the last line "this.name = name" still necessary? I think "this(name, 0)" already does that, right?

  31. Siddhant Das June 10, 2019 at 7:32 pm - Reply

    does this cover constructor overloading?

  32. JurajIsamkral June 10, 2019 at 7:32 pm - Reply

    very nice tutorial, thank you Cave

  33. Cave of Programming June 10, 2019 at 7:32 pm - Reply

    Cheers. You want to call various constructors from other constructors? You can call whichever constructor you like from another constructor. The only rule is that you can only call one constructor from another, and it must be the first line in the constructor. But different constructors can call different other constructors. Try it and see.

  34. Priyank Kharat June 10, 2019 at 7:32 pm - Reply

    Awesome tutorials understand much better than my college!!!! Thanks for awesome teachin..
    One doubt….
    Can we call more than one constructor using "this" from other than two constructors ??

  35. Iomega311 June 10, 2019 at 7:32 pm - Reply

    I paid over $100 for my Java book… and your videos teach these topics much, much better. Thank you for taking the time to teach all of us this language!

  36. Cave of Programming June 10, 2019 at 7:32 pm - Reply

    It has two constructors by the end of the video; one of them takes only a string, the other requires a string and a number.

  37. tacky tacks June 10, 2019 at 7:32 pm - Reply

    Why is this(name , 0); necessary in public Machine(String Name){..}?

  38. Cave of Programming June 10, 2019 at 7:32 pm - Reply

    It's a pleasure 🙂 The buttons and stuff that you add in the constructor of your window class would have their own methods that would then be invoked automatically when they're clicked, so for that reason you wouldn't necessarily need to refer to the main window class again.

  39. 123japanuser June 10, 2019 at 7:32 pm - Reply

    This is a very crucial topic and its used daily in my production code.

  40. unclvinny June 10, 2019 at 7:32 pm - Reply

    Ok, that helps. It seems like I wouldn't have a way to get a handle on those panels, windows and widgets without going through a specific MainFrame object later, like myMainFrame:

    MainFrame myMainFrame = new MainFrame();

    … but I'm sure this will all make sense later. Thanks again! I'm really liking the series.

Leave A Comment

*