Isn't it enough to just create a setTime method assigning the hour, minute, and second variables by itself? Instead of creating set-Hour, -Minute, and -Second methods, I mean.
after watching this video 3 times i finally understood something : the "this" function or whatever it is jut calls the constructor again with the arguments given in the parenthesis and if the user only enters the hour then by default it will go to the constructor with 3 arguments because of the code: this(h,0,0);
actually calling another method in a constructor is not recommended. this is because fields are still being initialised and all fields required by method might not have been set yet. so instead of using setTime(h,m,s); in constructor just make the 3rd constructor as-
public tuna(h,m,s){ this.hour=h; this.minute=m; this.second=s; }
If you are stuck, remember to search different videos on the topic, like https://www.youtube.com/watch?v=YqBVgUh64Ks in this case. It helps to hear an explanation from different sources.
Thanks Bucky!!! This is my second day watching this tutorial and practicing. I am in lesson 39. I think I should take a break and sleep over it because it is getting complicated in my mind.
this : points the variable in the current class; this(): it invokes the current class constructor. If this(a), it look for the constructor with one parameter, this(a,b) for two parameters and so on. Calling this(a,b,c) in default or constructors with less parameter is pretty rare.
Having a problem, please help!! This piece of code I put below is exactly the same as his, but on my screen it has a red error message saying "recursive structure invocation" Anybody know what the issue is?
I may be wrong, but i think the "this" on this situation only works because of the overload constructors having the same name "tuna". As he shows, it gave erros until he created the fourth constructor. Anyway, I wouldn't use this. Just skip to the next class (getters and setters) and use that instead.
so, I messed up somewhere and tried to add/make a new project with multiple classes… but all of them say, "package <insert name for package>;" I don't expect a reply (because old video 🙁 but it'd be nice) how do I remove that without breaking my code? or am I hosed?
OK, if I have a constructor with variable-length argument (e.g: (int…x)), and I build more constructors, which one will the program choose? Or will it give an error?
Easiest way of demonstrating how each of the constructors will work based on how many parameters you use. Remember we can assign values for constructor parameters by doing myclass class obj = new myclass(parameter).
class myclass { private int a; private int b;
public myclass() { System.out.println("This is the first constructor, no args"); }
public myclass(int a) { System.out.println("This is the second constructor, one arg " + a); }
public myclass(int a, int b) { System.out.println("This is the third constructor, two args " + a + b); }
public static void main(String args[]) { myclass classobj = new myclass(); // uses the first constructor myclass classobj2 = new myclass(1); // uses the second constructor myclass classobj3 = new myclass(1,2); // uses the third constructor } }
What if you wanted one of the parameters to be random? How would you do this using a new constructor to call an already made one by the this() statement?
I've tried writing out a random number generator but ran into an error that said the "this" statement must be the first line.
Thanks! And if you don't get around to this question, thanks anyway for your vids!
So far I've understood everything in your tutorial series of Java perfectly (I believe), until now. I just don't get the how it works. Why having so many constructors and what are their purpose? Thanks in advance for any answers!
If anyone is watching this and is confused I recommend this video: https://www.youtube.com/watch?v=T9fRL52TP_M
Isn't it enough to just create a setTime method assigning the hour, minute, and second variables by itself? Instead of creating set-Hour, -Minute, and -Second methods, I mean.
Guys this is like super?
after watching this video 3 times i finally understood something :
the "this" function or whatever it is jut calls the constructor again with the arguments given in the parenthesis and if the user only enters the hour then by default it will go to the constructor with 3 arguments because of the code: this(h,0,0);
3:24 the errors doesnt dissapear after ive created the constructor
copied the code straight from the video so does anyone have a clue?
3:14 Well if I knew that getting rid of my exes was that easy I've started learning java sooner)))
Java is just beautiful!
actually calling another method in a constructor is not recommended. this is because fields are still being initialised and all fields required by method might not have been set yet.
so instead of using setTime(h,m,s); in constructor
just make the 3rd constructor as-
public tuna(h,m,s){
this.hour=h;
this.minute=m;
this.second=s;
}
Not gonna lie. I used to complete 10 tutorials a day at first. But now it's only 5 tutorials and I got exhausted.
This is better and has better use of this.
public class Tuna {
private int hour;
private int minute;
private int second;
public Tuna () {
}
public Tuna (int hour) {
this.hour = hour;
}
public Tuna (int hour, int minute) {
this.hour = hour;
this.minute = minute;
}
public Tuna (int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void setTime(int hour) {
this.hour = hour;
}
public void setTime(int hour, int minute) {
this.hour = hour;
this.minute = minute;
}
public void setTime(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
If you are stuck, remember to search different videos on the topic, like https://www.youtube.com/watch?v=YqBVgUh64Ks in this case. It helps to hear an explanation from different sources.
Copied this verbatim and it doesn't work… Errors galore.
can you do?:
public tuna(int…unitsOfMeasurements)
Instead of creating so many constructors why can't we use variable length arguments?
You are the best man. i really liked your tutorial . System.out.println("Thank you ")
Thanks Bucky!!! This is my second day watching this tutorial and practicing. I am in lesson 39. I think I should take a break and sleep over it because it is getting complicated in my mind.
this : points the variable in the current class; this(): it invokes the current class constructor. If this(a), it look for the constructor with one parameter, this(a,b) for two parameters and so on. Calling this(a,b,c) in default or constructors with less parameter is pretty rare.
for those who didn't understand at 4:24 , he meant "where the fuck is that method"
yeah you're welcome
What I learned in Computing Class 1 for 2 hours is condensed in 8 mins in this video. Superb work.
https://www.geeksforgeeks.org/constructors-in-java/
oh man,after watching Bucky‘s tutorials ,any programming language is a piece of cake
Having a problem, please help!!
This piece of code I put below is exactly the same as his, but on my screen it has a red error message saying "recursive structure invocation"
Anybody know what the issue is?
public tuna(int h, int m, int s){
setTime(h,m,s);
}
people be complaining, but bucky at least explained it in some way. my teacher at uni just showed us a ready-made program and told us to copy it lol
You are like professor Leonard of programming, & thats why I love you and your jokes so much… thank you so much for saving my life…
This keyword basically calls the same class constructor with the matching number of arguments
Example:
this(1,2,3);
calls the tuna constructor with the 3 arguments
this(1,4,2,3);
calls the tuna constructor with the 4 arguments(if existing)
Lost me
Why did he use this without a dot seperator in this example instead of using the setTime() function throughout?
I may be wrong, but i think the "this" on this situation only works because of the overload constructors having the same name "tuna". As he shows, it gave erros until he created the fourth constructor.
Anyway, I wouldn't use this. Just skip to the next class (getters and setters) and use that instead.
Constructors are not methods!
3:16 "and if you watch all my exes just disappeared" that caught me
this way will bring you an overridable method setTime in tuna
so, I messed up somewhere and tried to add/make a new project with multiple classes… but all of them say, "package <insert name for package>;" I don't expect a reply (because old video 🙁 but it'd be nice) how do I remove that without breaking my code? or am I hosed?
all his exes dissapeared
OK, if I have a constructor with variable-length argument (e.g: (int…x)),
and I build more constructors,
which one will the program choose? Or will it give an error?
every end of video he promises to explain better. I am still waiting…………….
Bucky went from being one of the better teachers to one of the worst holy shit these vids are getting bad lol
whats the point of using this(0,0,0), this(h,0,0)…in every constructor function???
I still dont understand why need put sethour setmin setsec
Why cannot directly
hour=h;
minute=m;
second=s; like the tutorial be4
Easiest way of demonstrating how each of the constructors will work based on how many parameters you use. Remember we can assign values for constructor parameters by doing myclass class obj = new myclass(parameter).
class myclass {
private int a;
private int b;
public myclass() {
System.out.println("This is the first constructor, no args");
}
public myclass(int a) {
System.out.println("This is the second constructor, one arg " + a);
}
public myclass(int a, int b) {
System.out.println("This is the third constructor, two args " + a + b);
}
public static void main(String args[]) {
myclass classobj = new myclass(); // uses the first constructor
myclass classobj2 = new myclass(1); // uses the second constructor
myclass classobj3 = new myclass(1,2); // uses the third constructor
}
}
awesome tutorials..u cn learn new concept each time in short duration
can't this be done using "variable length arguments" instead of creating so many constructors
So bucky is fluent in every possible programming language there is, but does not know how to use ctrl+c.
What if you wanted one of the parameters to be random? How would you do this using a new constructor to call an already made one by the this() statement?
I've tried writing out a random number generator but ran into an error that said the "this" statement must be the first line.
Thanks! And if you don't get around to this question, thanks anyway for your vids!
why the fuck is this giving error
????
public class t39 {
private int hour;
private int minite;
private int second;
public t39(){
this(0,0,0);
}
public t39(int h){
this(h,0,0);
}
public t39(int h,int m){
this(h,m,0);
}
public t39(int h,int m,int s){
this(h,m,s);
}
}
I didn't get that .. why he didn't used this(h , m , s);
in 4th constructor too..??
LOL dis guy said: "but the meat of this video is here…"
So far I've understood everything in your tutorial series of Java perfectly (I believe), until now.
I just don't get the how it works. Why having so many constructors and what are their purpose?
Thanks in advance for any answers!
Thank you BUCKIE!!
Your the best bro i watched the c++ videos and now im learning java!