Java Programming Tutorial - 38 - Public, Private and this

Поділитися
Вставка
  • Опубліковано 12 гру 2024
  • Source Code: github.com/the...
    Core Deployment Guide (AWS): docs.google.co...

КОМЕНТАРІ • 410

  • @filipvukovic8009
    @filipvukovic8009 11 років тому +292

    The stuff he says about "private" and "public" are correct. But he made a mistake when talking about "this". What "this" really is in Java is an object for which you call a method. In this here example "this" is actually tunaObject, because you are using tunaObject to call a method named setTime. It has nothing to do with local variables.
    Example1:
    public void setTime(int h, int m, int s)
    {
    hour=h; // same as this.hour=h;
    minute=m; // same as this.minute=m;
    second=s; // same as this.second=s;
    }
    Example2:
    public void setTime(int hour, int minute, int second)
    {
    this.hour=hour;
    this.minute=minute;
    this.second=second;
    }
    in Example2 you have to use "this" because your formal arguments are named the same as attributes of the tuna class. It wouldn't make any sense to write hour=hour. The only way to access your attributes in these situations is to use "this". Hope I helped a little to clear this up. :)

    • @achauhan008
      @achauhan008 10 років тому +9

      Thanks man. Cheers!

    • @amatorif
      @amatorif 10 років тому +8

      Thank you very much that makes much more sense now!

    • @MaYukun
      @MaYukun 10 років тому +3

      Appreciate a lot! I was quite confusing when watching video.

    • @Earlosaur
      @Earlosaur 8 років тому +4

      +Filip Vuković wow this small explanation you took the time out of your day to write really cleared it up for me. thank you sir!

    • @Pbd-fl1sx
      @Pbd-fl1sx 7 років тому +1

      Thank you very much!

  • @Gelicania
    @Gelicania 13 років тому

    I'm a beginner, because of the comments I was not able to watch the video. But then again I'll finish all your tutorial. Your tutorials amaze me, so one mistake will not affect the whole tutorials.

  • @Daijyobanai
    @Daijyobanai 15 років тому +1

    Bucky, if you think these thises are confusing you should have heard this from my teacher here at school. You have made clear in under 6 mins what he couldn't make me understand in 3 months. THANK YOU for these tutorials, you have taught me much more than the stoopid jargon-filled textbook or my teacher. Again, THANKS!

  • @mikel3507
    @mikel3507 9 років тому +138

    Warning! "this." is not explained correctly in this tutorial. When you use "this." you are referring to the "private int hour = 1;", and not to "setTime(int hour..."

    • @PixeletSushi
      @PixeletSushi 9 років тому

      +Igor Galandák
      Thanks a lot that helped so much

    • @EasternUNO
      @EasternUNO 8 років тому

      +Igor Galandák thank you, I understand it finally :D so when you use this keyword, you are reffering to global variable which is inside your TestThis class. If you don't use this keyword, than here will be used parameter you will enter in your main class right?)

    • @woodwoody9083
      @woodwoody9083 7 років тому +3

      It's okay man. Bucky is also a human and he has tried his best. :)

    • @prasannaraghava23
      @prasannaraghava23 7 років тому

      Thanks, Igor Galandak, This example is helpful.

    • @isaacchen6630
      @isaacchen6630 7 років тому +1

      Sorry, I don't pay attention to warnings. only errors.

  • @montygupta7089
    @montygupta7089 8 років тому +8

    ATTENTION!!! when we use 'this. ' . It will generally refers to data member of class. with using ' this.' if any change is made this will reflect to data member of class not local variable to method(function).

  • @sadokdanger
    @sadokdanger 8 років тому +14

    Since bucky's explanation of the keyword "this" is wrong, I made a simple program to make things more clear because I was really confused at first:
    public class test{
    int a=5;
    void print(int a){
    System.out.println(a);
    System.out.println(this.a);
    }
    public static void main(String args[]){
    test object=new test();
    object.print(3);
    }
    }
    The output of this program would be:
    3
    5
    So inside the method print, a refers to the local variable and this.a refers to the instance variable.

    • @tayyabarshad7330
      @tayyabarshad7330 8 років тому +1

      More Simple:-
      public class test{
      int a=5;
      static void print(int a){
      System.out.println(a);
      System.out.println(this.a);
      }
      public static void main(String args[]){
      print(3);
      }
      }
      The output of this program would be:
      3
      5
      So inside the method print, a refers to the local variable and this.a refers to the instance variable.

  • @dragonsteria3042
    @dragonsteria3042 9 років тому +113

    amm "this" is exactly the opposite... when using "this" u are referring to the global variables and not the arguments.

    • @jsdsz
      @jsdsz 9 років тому +12

      u mean instance variables. global variables are generally out of the class while instance variables are within the class but not in any method...if i'm not wrong global variables are used in c/c++ as in java everything takes place within the class...and i think u are right "this" refers to the instance variable and not the local variable

    • @AyushkaPartohap
      @AyushkaPartohap 8 років тому +8

      +DRAGONSTERIA Thank you. I thought I was going crazy.

    • @ShuklaPaksha
      @ShuklaPaksha 8 років тому +6

      EXACTLY! He just reverted the use of this. I wonder how he explains the use of variables in the other functions of the same class. MAN! I was going crazy. +thenewboston please please, consider redoing this video.

    • @robertreimann1846
      @robertreimann1846 8 років тому

      I realized that cause I know some c++. Bucky is so stupid xD

    • @johncena-mw3ge
      @johncena-mw3ge 5 років тому +1

      exactly he told just the opposite!!! Now i am questioning his other videos!!

  • @tomwilliams7326
    @tomwilliams7326 8 років тому +1

    "this ones," You have got me ROLLING! it is late and i've been watching java videos for hours, and your humour is killing me, haha!

  • @jvsnyc
    @jvsnyc 9 років тому +126

    Your overall series is so good, and this one is so confusing due to the incorrect explanation of this, that you should consider redoing this, posting a warning, or just deleting this one. The series is one of the better ones for beginners except for this.

    • @creativegoods7737
      @creativegoods7737 7 років тому +2

      best*

    • @fromika
      @fromika 7 років тому

      this need better explanation, might some drawings could be used to explain. In general the videos are not bad but not THIS. Good luck!

    • @YM-vn2ts
      @YM-vn2ts 7 років тому +8

      can this comment use any more this

    • @brunoribeiro1383
      @brunoribeiro1383 6 років тому +2

      You guys can complain as much as you want, but this video still better than my teacher's explanation

    • @FF-ie6sd
      @FF-ie6sd 6 років тому +2

      @@brunoribeiro1383 the point is he made an mistake. It has nothing to do with your teachers explanation.

  • @jonn9302
    @jonn9302 11 років тому +22

    I'm sure he used that tone:
    "Use this one, right here!" lmao

  • @AlexanderMccarthey87
    @AlexanderMccarthey87 11 років тому +8

    This is essential. Watch it a few times

    • @anarki777
      @anarki777 8 років тому +5

      essentially wrong.

  • @TheMeguyvor
    @TheMeguyvor 8 років тому +34

    Excuse me Bucky, wrong explanation as "this" refer to the variable of the current class, not the argument. :)

  • @grymmatarmar
    @grymmatarmar 11 років тому +9

    I think he meant the opposite. Because saying use these locales instead. That wouldn't change the value. So what it does is: Default: It checks the local variables. If you use 'this' it will check for the variables outside of the local scope

    • @EJMedia1
      @EJMedia1 11 років тому +1

      Exactly and if you name the local variable the same name as the variable in the class level scope then the compiler gets confused. I kind of like to think of the parameter in the parens as the temp variable which is trying to pass the variable from the local variable to the class level variable. We have to think of the compiler as stupid or is it the other way around?

  • @xSabretoothx15
    @xSabretoothx15 9 років тому +10

    I watched Bucky's Java series many years ago and it helped greatly in getting me started for becoming the developer I am today, but this particular video really screwed up my understanding of local vs. instance variables and the "this" keyword for awhile. Watch with caution, and read the comments.

  • @VEGETADTX
    @VEGETADTX 12 років тому

    Upravo tako kolega :)
    Ja bas gledam sta prica...zbunio sam se, ali dobro mora i on da se zbuni nekad :) Inace su mu fenomenalni tutorijali!

  • @skdkim
    @skdkim 9 років тому +1

    Good explaination of public, private and this! really cleared things up for me. Thanks!

  • @MrYouMath
    @MrYouMath 13 років тому +1

    Loled at 0:23: "Now what private means is that these variables are private.". Nice one :D.

  • @daa89
    @daa89 6 років тому +1

    I'm new to Java(obviously, that's why I'm here), but as far as I see it, in the first example, where the arguments have the same name as the global variables hour/minute/second, calling the method with arguments that are variables(e.g.: tunaObject.setTime(var1, var2, var3);) what the code would do, would be to set the values of the arguments to 4,5,6, but would not change the global variables that are used in the other class methods; that's why the display method still shows 01:02:03, because it is using the global variables.
    In case that didn't make any sense, this is what I mean:
    private hour, minute, second;
    public void setTime(int hour, int minute, int second)
    {
    hour=4;
    minute=5;
    second=6;
    }
    setTime(a,b,c);
    this would result in setting a=4, b=5,c=6.
    using the "this" keyword seems to make sense only in case the arguments' name match the names of some global variables(I call the global because they are global to the methods inside the class, although they are local to the class).
    Also, I'm not sure, but I would've somehow expected that calling the method with constants setTime(1,2,3); to result in a compilation error because it would technically mean executing 1=4;2=5;3=6;

  • @DeanDip
    @DeanDip 13 років тому

    For people that are confused, Bucky explained it backwards. "This." before the variable refers to the global variables (I guess they're called instance variables, but I'm not good with all the programming terminology), without "this." the local variable is used. I remember there was a top rated comment that explained it before, but other comments have taken its place.

  • @paulrudman1349
    @paulrudman1349 5 років тому

    Beautiful clear explanation of 'this', but it works the other way around!

  • @Kaysar777
    @Kaysar777 10 років тому +2

    if method arguments have the same name as the name of the variables in a class, then it assumes that it is working with its arguments and not variables, that's why when we change values in the method it doesn't have any effect on values of the variables. But when we use "this" keyword it explicitly states that we want to set the values for instance (object) variables.

  • @thomasp3218
    @thomasp3218 9 років тому +28

    Funny, I started watching these videos in college and now some years later, this vid is still not fixed :P

    • @geekyloopholesfinder
      @geekyloopholesfinder 9 років тому +6

      Tom Tom are you working as a developer now ? after college ?

    • @MustaWizard
      @MustaWizard 7 років тому +2

      so you need to watch a beginner guide years after college?

    • @valizeth4073
      @valizeth4073 6 років тому +2

      It's always good to go back to the basic. There are always things you've forgotten, or need another clearification. I do it all the time with C++. Sometimes I forget the correct syntax for operator overloading etc.

    • @nayaaccount5575
      @nayaaccount5575 6 років тому

      ya not still

    • @hetaeramancer
      @hetaeramancer 3 роки тому

      How will you fix an already uploaded video dumbass...

  • @71Eclipse17
    @71Eclipse17 11 років тому

    I don't see why they dislike those videos. They are TOO GOOD!

  • @franciscoecabrera
    @franciscoecabrera 9 років тому +6

    ... "this" doesn't make sense!! but you know what i mean...
    when you play a round a little bit, you are gonna undestand what "this" means...
    "this" is a kind a little joke!! XD
    No matter "this" error, but still he is a good teacher!!

  • @xrtze
    @xrtze 14 років тому

    just great how you make us understand this, 5 books couldn´t tell me as good as you.
    regards.

  • @MVSoler
    @MVSoler 11 років тому

    yeah, you are correct. This refers to the members of the class. If not, it is impossible to show "4,5,6", because toString or toMilitary are refer to the variables of the tina class.

  • @zaneizane
    @zaneizane 15 років тому

    I understood the concept that you were putting across awesome once again cheers : )

  • @ljka
    @ljka 6 років тому

    'this' is for the accessing instance variables such that that these variables (or properties) are attached to the objects not to the class itself. Local variables on the other hand are variables locally created, used and destroyed inside methods. And lastly, we have the so called, class variables, which are attached to the class itself and therefore common throughout all instance of the class -- we use 'static' keyword for this kind of variables. Additionally, static methods can only use static variables.
    Above is what I understand based on what i know about OOP PHP and Java. Feel free to correct. Thanks! :)

  • @CodeBubba
    @CodeBubba 10 років тому +1

    Yeah, Bucky, I agree with Indrajeet. This is going to confuse a new developer a bit. You are stating that this.variable is referring to the formal parameters which is incorrect. this. is referring to those private instance variables.

  • @itachi2011100
    @itachi2011100 10 років тому +1

    i like your sense of humor and pun

  • @MohaSharpeyes
    @MohaSharpeyes 14 років тому

    exactly what specimon said
    love you bucky keep em coming please

  • @alvin2014
    @alvin2014 11 років тому

    It's people like you that make open courses work :)

  • @mzpter
    @mzpter 12 років тому

    yes, this document is correct, this keyword always refers to the object itself, so writing this.x inside a method addNumbers(int x, int y) does not refer to the parameter x but the object's field named x. If you would use only x instead of this.x, it would be a reference to the method's parameter x.
    I wrote an example:pastebinDOTcom/pxdjbPuJ

  • @jgmacho
    @jgmacho 9 років тому +8

    He made a mistake. The "this." refers to the field variables that are private not the arguments in the method. Basically this allows a method to change the value of the private field variables after the method has ran. The IDE shows the relationship while he was explaining. Also, I believe the field variables are not referred to as local variable. Local variables are variables that are only available to a specific method. Variables for a class are called fields. They are the ones marked private or public.

    • @rrc5982
      @rrc5982 6 років тому

      Couldn't you still change the private field/global variables by having them in the method but not have the same name as the parameter variable in the method and therefore not need to use 'this'?

  • @soujanyav8443
    @soujanyav8443 9 років тому

    I understood "this" significance pretty well here!! very well explained :)

    • @Brax1982
      @Brax1982 9 років тому +3

      soujanya v You should not understand it, because this is completely wrong. The "this" keyword represents the enclosing object and grants access to the member variables, not the parameters of that set method. Eclipse will helpfully color them blue in this case.

  • @TReXcuRRy
    @TReXcuRRy 5 років тому +1

    this.hour means you refer to the *INSTANCE VARIABLE* hour, *NOT THE ARGUMENT* hour.
    Example :
    public class A {
    public static void main(String[] args) {
    B BO = new B();
    BO.setCalc(3);
    BO.print();
    }
    }
    public class B {
    private int number=2;
    public void setCalc(int number) {
    this.number = number;
    }
    public void print() {
    System.out.println(number);
    }
    }
    This program returns 3.
    If you remove "this.", it returns 2 (setCalc basically does nothing).

  • @FALCASANTOSDENNISNATIVIDADfalc
    @FALCASANTOSDENNISNATIVIDADfalc 6 років тому

    man 2009 really? you still helping me out right now, even it is 2018, you are explaining it like my friend instead of a teacher.

  • @k98killer
    @k98killer 11 років тому +19

    I think he said the opposite of what he meant.
    this.variable is the object property, will variable is the local variable in the method scope.

    • @HammaLlama
      @HammaLlama 10 років тому +7

      You're right, this.variable refers to the class variables, not the method variables.

  • @Cyphlix
    @Cyphlix 12 років тому

    Im glad of yahoo answers / the top comments explaining the difference properly, this video defiantly got it wrong - despite how good bucky's vids are in general :)

  • @bangalirussian
    @bangalirussian 12 років тому

    ok if anyone is still confused bout "this", here is another explanation. "this" refers to the private variables of the tuna class. If "this" wasn't used, instead of refering to the private variables, the program would create local variables inside the method setTime, because the arguments for the method had the same name as the private variables.

  • @andriusrimkus7743
    @andriusrimkus7743 11 років тому +10

    I have a feeling you mixed something there, Bucky. :)

  • @anarki777
    @anarki777 8 років тому +22

    Glad I understood this properly before watching this video, because this is wrong.

    • @ZiiiP2142
      @ZiiiP2142 8 років тому +4

      It was messy, but the IDE word coloring saved the day.

    • @YouTuber-dz6jh
      @YouTuber-dz6jh 7 років тому

      same here

    • @MrBunny53
      @MrBunny53 7 років тому +3

      what's wrong about this? :O

    • @YouTuber-dz6jh
      @YouTuber-dz6jh 7 років тому +4

      if a method has parameter called " a " and the class has a field member called " a " also then if you typed "a" in the method body it will refer to the "a " parameter not the field member so when using this.a it ensures to select the field member "a " not "a" parameter .
      "this" keyword a generic way refer to the instance of the class the " object " .
      hope it is clear to understand

    • @valizeth4073
      @valizeth4073 6 років тому

      Generally it's considered bad practice to name two different variables the same name, but the "this" keyword is really useful anyways so you make it clear that you're refering to the member variables.

  • @franciscoecabrera
    @franciscoecabrera 9 років тому +4

    "if you use a little code this... it's gonna refer to this variable..."
    WRONG: he selected the "int hour" from setTime method, instead of the instance "private int hour" declared below the "public class tuna". And another funny thing is that when he moves the TEXT_INSERTION_POINT over the VARIABLE that he is talking about: the eclipse IDE makes a SHADOW with color GRAY to the correct referencing VARIABLE !! XD

  • @nitishchauhan8655
    @nitishchauhan8655 9 років тому +18

    this video is incorrect about this. operator.
    using his code...
    setTime(int x,...)
    {this.x = 3}
    This will set the object's x value to 3 not the parameter. You could do setTime(4,...) and it would still place a 3 in tunaObject.x

    • @geekyloopholesfinder
      @geekyloopholesfinder 9 років тому +4

      Nitish Chauhan can you explain me what are u talking about ?

    • @sankamohottala6778
      @sankamohottala6778 6 років тому

      you mean {3=this.x} . only this way we can change a parameter value to something else.what this.x =3 does is parameter overriding the instance variable.

  • @TheMotlias
    @TheMotlias 11 років тому

    oh bucky, i love that you do thease tutorials

  • @HarshGames
    @HarshGames 7 років тому +1

    Love the comment section! Least toxic on youtube and most helpful!!!

  • @OfficialWingzzz
    @OfficialWingzzz 11 років тому

    I'm replying real late hehe, anyways I hope you figured it out, but if not here you go: the keyword 'this' is normally used when a parameter is the same name as a field that you want to use (mostly used in constructors to assign the instance a value for a field) and you're going this.hour (hour is say a field in the program) so this.hour = hour, (hour could be a parameter in a method or a constructor) and so now if we use the constructor or method we assign a value to our field "hour" to use.

  • @rushikeshdate4034
    @rushikeshdate4034 9 років тому

    Thanks you cleared my concept of 'this'.

  • @rappurappu
    @rappurappu 9 років тому +5

    For those whom are confused :
    class TestThis{
    private int x=1;
    public void setX(int x){
    System.out.println(this.x);
    System.out.println(x);
    }
    public int getX(){
    return x;
    }
    }
    class Start{
    public static void main(String[] args){
    TestThis t = new TestThis();
    t.setX(5);
    }
    }
    The output is:
    ==========
    1
    5

  • @r8bior
    @r8bior 7 років тому +1

    You're such a funny guy!!!
    Love your vids :D

  • @FALCASANTOSDENNISNATIVIDADfalc
    @FALCASANTOSDENNISNATIVIDADfalc 6 років тому

    I understand already within 1:28, thank you very much.

  • @pekwk45
    @pekwk45 12 років тому

    "this" used when local and stack variables have the same name. Helps to identify as well as reduce the amount of distinct variables used, if the program is large, it makes the difference.

  • @arvindr21
    @arvindr21 15 років тому

    The usage of 'this' in the method:
    public void setTime(int hour,int minute,int second){
    this.hour=hour;
    this.minute=minute;
    this.second=second;
    }
    Initialize the values in the 'main'

  • @d2smwar
    @d2smwar 13 років тому

    in addition to revising your explanation, try this example:
    public void setTime(int hour,int minute,int second){
    hour = 3;
    minute = 4;
    second = 5;
    this.hour = hour;
    this.minute = minute;
    this.second = second;
    }

  • @janeuarymmiv
    @janeuarymmiv 15 років тому

    This will definitely can help me later on. when i started to study again^_^ more tutorials

  • @danieljohnson265
    @danieljohnson265 9 років тому +35

    It seems he used 'this' correct in practice but he explained the theory incorrectly.

    • @FirstNameLastName-tc2ok
      @FirstNameLastName-tc2ok 7 років тому +1

      Daniel Johnson can you explain how he used it correctly? From what I understand, his use of it aligned with the way he explained it (although from the comments I know it is incorrect)

    • @johncena-mw3ge
      @johncena-mw3ge 5 років тому

      Practically also he taught the opposite!!

  • @KE61090
    @KE61090 14 років тому

    " this" either refers to an instance variable or it refers to the appropriate constructor in the class based on the number of args passed in. it doesn't refer to the method variables or "local" variables.

  • @rappurappu
    @rappurappu 9 років тому +9

    I am a big fan of your. There is no doubt that you are doing great job but this video is incorrect. "this.fieldName" always refers to the instance's field not the local field.
    I know you know this but you missed the point here. Hope you will clear this up.
    Thanks

  • @alvin2014
    @alvin2014 11 років тому

    Well if the people that disliked the video were paying attention, he catches himself at the end. Fastest clean-up i've ever seen lol

  • @Warioproductions
    @Warioproductions 11 років тому

    Bucky accidently switched names, so here's the explanation:
    - Instance variables are declared outside of a method (so between the class brackets), local variables are those declared in a method (and thereby are only visible in the method they're declared in). The this keyword is used to refer to the instance variables.
    So this.hour = hour means, that we assign the value of local variable hour (on the right) to the instance variable this.hour (on the left).
    Regards! Hope it helped a bit.

  • @JamalKhaffaf
    @JamalKhaffaf 12 років тому

    when you write "this.hour" actually it refers to the class variables not the method variable. dont get confused. if you wanna see it your self always use print statement and u will see what it will print. PLUS I WANNA ADD A NOTE "if you noticed when the cursor was on this.hour it highlighted hour variable of the class not of the method. dont get confused.
    Good luck guys.

  • @jdubeau007
    @jdubeau007 14 років тому

    "this" refers to current object which in this case is the tuna class.
    You can see Eclipse highlighting the fields of the current object.

  • @bmarkrussel
    @bmarkrussel 15 років тому

    now i understand what 'this keyword does...
    i really appreciate your tutorials..^^
    '

  • @SIlentZedrikMusic
    @SIlentZedrikMusic 4 роки тому

    I'VE LEARNED ALOT. THANKS!!!

  • @azalru
    @azalru 7 років тому +6

    Bucky was drunk when he did this!

    • @michaelli3997
      @michaelli3997 7 років тому +1

      LOL dat's exactly what I thought!

    • @john_Reilly2027
      @john_Reilly2027 6 років тому +1

      So whats new .. he is drunk in all his tuts if you listen to how he slur's his words all the time .. but still the best tuts on youtube "that's what she said"

    • @valizeth4073
      @valizeth4073 6 років тому

      I prefer cherno's C++ tutorials more since he usually goes a bit more in depth about certain things. But for java as of now Bucky's doing it really good. Besides the "this" mixup.

  • @cwieand
    @cwieand 15 років тому

    I agree as well with specimon "this" refers to the the instance variables.
    In my class they taught that it will actually do exactly the opposite of what you said it would in the video. It will consider the variables local to the method first then only the instance variables inside the method if you specify with this.second ect.
    Love your videos. They help me to review topics and get extra practice but this appears to be incorrect.

  • @aswinkharel2737
    @aswinkharel2737 9 років тому +1

    Bucky "this" operator dereferences the pointer to object to which the function is being called. Basically this.hour means the instance variable of the object not the arguments passed while calling the function. Similar to self in objective c, (*this) in c++

  • @surajsri248
    @surajsri248 7 років тому

    this keyword is used for calling data members not local variable.....u figured it out little wrong.....but u r doing a great job...

  • @Gemmellness
    @Gemmellness 12 років тому

    Actually it does divide by 12 but takes the remainder as the output of the function, so 8/12 is 0 remainder 8. 15/12 is 1 remainder 3, so 3 is the output.

  • @Darker_Void_Scientist
    @Darker_Void_Scientist 13 років тому

    @matthewdoucette this is not the class, this is the object . For example (class)String j(object).
    Contstructor(String j){
    this.j = j;
    }
    this.j is not the same as String.j .this is a flag referring to an object ,or variable as in int j.

  • @kresimirkovacic
    @kresimirkovacic 12 років тому

    i really think you mixed it up when you described 'this'.
    the first time (when it was hour=3) it actually refered to method variables, so basicly it didn't do anything because those variables are not used later (class variables are used)
    and the second time it refered to class variables (when you put this.hour), and it made a difference because we changed variables that were used later when printing time.

  • @kenjiokagmail
    @kenjiokagmail 15 років тому

    rajeev, you are right. This tutorial was so far so good. But because of THIS.....

  • @waqqas_the_wicked
    @waqqas_the_wicked 14 років тому

    When we say:
    this.hour = it means the hour variable of THIS class and not the hour variable in the parameter list.
    You've said the opposite in the video.
    But hey, we all make mistakes and we all appreciate your effort :)

  • @specimon
    @specimon 14 років тому

    @JamesBecht :
    Thats actually what I said. "This" refers to the object and therefore the object variables. Not the method or constructor variables...

  • @k022002
    @k022002 13 років тому

    I dont know Y or people rush to criticize...
    I bet none of them try or do what u've just done.
    I GOT Something From this! THANKS BUCK..

  • @baietzashu94
    @baietzashu94 6 років тому

    i came here only for " this. " ...and now i know what that means...Thank You!!

  • @zO0pi
    @zO0pi 7 років тому +2

    i am crying with "this" tone :'D

  • @mzpter
    @mzpter 13 років тому

    @mzpter the best example of using "this" would've been if you wrote this.hour = hour; this.minute = minute; this.second = second; in the setTime method. So that you assign the values of the parameters to the class variables with exactly same names. (As you can not write hour = hour). Nice tutorials by the way. :)

  • @chelseybakerxx
    @chelseybakerxx 11 років тому

    Bucky is the best! But unfortunately, the dislikes of this tutorial are because Bucky has "this" backwards. "this" refers to the current instance of the class. So, "this.hour" will be the "hour" that is used throughout the entire class. You can see this when he runs the program. The setTime function has set the "hour" variable declared at the top of the class to to 4. the toString function grabs this same variable. The "hour" passed in the constructor is ONLY accessible within the constructor.

  • @majorgreenreader
    @majorgreenreader 12 років тому

    If you have multiple class files that measure the same thing, such as it needs to measure time or count the number of die rolls or something, you don't have to name each one "hour1, hour2, hour3". It makes it easier to manipulate the variables when you have more classes. (Someone please correct me if I'm wrong)

  • @bangalirussian
    @bangalirussian 12 років тому

    To clarify, the local variables would be created regardless whether they have the same name as the private variables. This is just a matter of ghosting.

  • @sergiumogan
    @sergiumogan 12 років тому

    You are a little bit out of course. "this" is the reference to the current class you are in to. In your example the code "this.hour" is equal to "tuna().hour" or to "tuna t = new tuna(); t.hour".

  • @Dimthegamer115
    @Dimthegamer115 11 років тому

    So basically the first run of it, it takes the value from this.hour before running the rest of the program thus whatever value is set in the setTime() method is not yet in use and then when you specify it as this.hour = hour; you are giving it a time and as such changing it. I am trying to work this out because it's late and i know i i don't I will forget to look again.

  • @soudehnilforoushan218
    @soudehnilforoushan218 8 років тому

    hi!! thank you so much your tutorials are great and intelligible

  • @rahular4596
    @rahular4596 7 років тому

    Before commenting don't forget that Bucky is also a human not robot,
    here bucky might be wrong but not always
    so guys go ahead and watch the playlist without any fear.......

  • @OfficialWingzzz
    @OfficialWingzzz 11 років тому

    "this", isn't actually referring to local variables. When he did this.hour, he referred to the class variable "hour". Meaning his parameters (the variables in his setTime() method) weren't being used at this point. Fix this by saying this.hour = hour; Meaning this.hour (class private variable hour) is now equal to whatever you specify in place of the parameter hour (variable in the method brackets -> setTime(int hour...). Sorry if you've found out by now, figured I'd mention it anyways!

  • @burak00
    @burak00 8 років тому +2

    Be careful, although the overall quality is very good. This video contains wrong information about 'this'.

  • @kresimirkovacic
    @kresimirkovacic 12 років тому

    jesu, sjajni su :) naučio sam javu prek njih

  • @uniquein2020
    @uniquein2020 9 років тому +7

    I think you should review this video because it's not right about "this" keyword means or does.
    & Great job BTW

  • @xammax117
    @xammax117 11 років тому

    You may have to initialise the variables inside the method call to setTime with your this.hour, this.minute, this.second in your main class. For example:
    Object.setTime(0, 0, 0);
    System.out.println(Object.toMilitary());

  • @1kalekip1
    @1kalekip1 9 років тому

    Everybody now knows that Bucky is indeed wrong: "this" refers to the class variables and without "this", it refers to the local variables.
    What I find funny is that Bucky missed the orangish highlighting like in 4:24 :)

  • @tayyabbintahir3005
    @tayyabbintahir3005 8 років тому

    bucky ! u just unexpectedly disappointed me for the very first time :O

  • @vividMario52
    @vividMario52 12 років тому

    Thumbs up for a new tutorial on the this keyword

  • @nayaaccount5575
    @nayaaccount5575 6 років тому

    this is used to access instance variable not variable in methods.
    class JBT {
    int variable = 5;
    public static void main(String args[]) {
    JBT obj = new JBT();
    obj.method(20);
    obj.method();
    }
    void method(int variable) {
    variable = 10;
    System.out.println("Value of Instance variable :" + this.variable);
    System.out.println("Value of Local variable :" + variable);
    }
    void method() {
    int variable = 40;
    System.out.println("Value of Instance variable :" + this.variable);
    System.out.println("Value of Local variable :" + variable);
    }
    }
    Value of Instance variable :5
    Value of Local variable :10
    Value of Instance variable :5
    Value of Local variable :40

  • @ooojunkdnaooo
    @ooojunkdnaooo 13 років тому

    Might be a good idea to talk about the concept of scope for this tutorial. Also, is "this" really needed if one is organized and uses different variable names for class level variables and variable in methods in the class?

  • @ikemefunaikemefuna
    @ikemefunaikemefuna 13 років тому

    BUCKY ROCKS!!!!
    My Coding Improved!

  • @dr.surinderkaur9220
    @dr.surinderkaur9220 7 років тому

    Bucky was drunk when he did "this" XD XD

  • @IsaacC20
    @IsaacC20 11 років тому

    But in the video we saw when he added "this." inside the setTime method, that method refers to the local variables.

  • @TheExtremeDevice
    @TheExtremeDevice 12 років тому +1

    "Jimmy" "Come here THIS second!"

  • @COD5252
    @COD5252 13 років тому

    "this" function is not necessary all the times ,just directly typing the method name will also do