Java Programming Tutorial – 42 – toString

///Java Programming Tutorial – 42 – toString

Java Programming Tutorial – 42 – toString

FavoriteLoadingAdd to favorites

Facebook – https://www.facebook.com/TheNewBoston-464114846956315/
GitHub – https://github.com/buckyroberts
Google+ – https://plus.google.com/+BuckyRoberts
LinkedIn – https://www.linkedin.com/in/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/
Twitter – https://twitter.com/bucky_roberts

Java468x60
By |2017-05-18T21:21:59+00:00May 18th, 2017|Java Video Tutorials|50 Comments

50 Comments

  1. Brandon Brown May 18, 2017 at 9:22 pm - Reply

    Your tutorials are so well done but they never work for me in eclipse. 🙁

  2. 胡育琪 May 18, 2017 at 9:23 pm - Reply

    Thank you for giving me class! I am a little confuse. Why it need "return" in toString method? I am confuse in this part, because I think the structure of this code is similar to the structure of time class (36-37). Thank you!

  3. Kristina J May 18, 2017 at 9:23 pm - Reply

    I followed your instructions and it all works, but I was wondering why do we have to create private ints and then their correspondent ints later which are public? I have played around with the code and if I get rid of ‘m,d,y’ and make ‘month, day, year’ public it shows as 0/0/0 so obviously you cant cheat the code, but I dont really understand why.

  4. adka datka May 18, 2017 at 9:25 pm - Reply

    So that’s like the predefined << in the C++ ostream class right?

  5. Sean Hubbard May 18, 2017 at 9:26 pm - Reply

    I have no idea why, but I get 4/4/6 not 4/5/6, even when 4, 5, 6 are the arguments.

  6. Keshav Sharma May 18, 2017 at 9:27 pm - Reply

    Please help me
    what if i declare 2 String Methods in potpie class
    then to whom the keyword "this" will refer to ?

  7. omer abbas May 18, 2017 at 9:28 pm - Reply

    what does this do?

  8. aassi22 May 18, 2017 at 9:28 pm - Reply

    Lmao make sure you spell contrtuctor wrong @ 2:21

  9. maagduskaa May 18, 2017 at 9:29 pm - Reply

    why we can’t just use this.toString() ??

  10. Moyashi Mugiwara May 18, 2017 at 9:32 pm - Reply

    Is there any difference between the System.out.printf("The constructor for this is %sn", this); compared to using
    System.out.printf("The constructor for this is %sn", toString()); ? They do the same since ”this” calls the method toString() but I was curious if there are situations where you would rather call the method itself instead of using this to call it.. Hope I am making some sense T_T. Thanks in advance!

  11. AmazingCraft0210 May 18, 2017 at 9:32 pm - Reply

    I don’t understand this

  12. stathis stathopoulos May 18, 2017 at 9:32 pm - Reply

    Day after Month after Year, damn America and your retarded system

  13. Asmaâ Fa May 18, 2017 at 9:37 pm - Reply

    hello and thank u for ur useful tutorials 🙂
    here goes my question and hopefully u see it in time :

    i have a colour proprety which is a a private int ( 4 colours in total each one has a specific name)
    now, we’re asked to use a toString method, how could a toString method be used to relate each int number to its String name?

    thank u !

  14. The Spankles May 18, 2017 at 9:40 pm - Reply
  15. DiamondGamer10234 May 18, 2017 at 9:42 pm - Reply

    Wait so the keyword this is a reference to potObject

  16. Arkone Axon May 18, 2017 at 9:44 pm - Reply

    I’m having a lot of trouble here with one part. When I type out:
    System.out.printf("The constructor for this is %sn," this);
    The compiler (Eclipse, latest version – 4.5.1) declares "Syntax error on token "this," delete this token. And of course without that token it still won’t work. What’s going on, and how do I fix this? :/

  17. Yura Er May 18, 2017 at 9:44 pm - Reply

    package bucky;
    import java.util.Scanner;
    import java.util.Random;
    class apples {
    public static void main(String args[]){
    potpie O = new potpie(4,5,6);

    }

    }

  18. AJD May 18, 2017 at 9:45 pm - Reply

    "make sure you spell it wrong" 2:20
    lmao

  19. Veda Varma May 18, 2017 at 9:45 pm - Reply

    Why do we use toString while we can directly print the output as system.out.printf("the constructor for this is %d/%d/%d", month, day, year)

  20. David Colmander May 18, 2017 at 9:45 pm - Reply

    This was great, exactly what I was looking for. Still lost but less than 10 min ago 🙂

  21. Ime Nebitno May 18, 2017 at 9:46 pm - Reply

    The fact that your class in whitch you store the year, month and day is called potpie amuses me

  22. RumikXxeno May 18, 2017 at 9:49 pm - Reply

    What’s up people! Welcome to your 40 second Java tutorial …. hmmm hmmm 40 secs 😀

  23. M R May 18, 2017 at 9:49 pm - Reply

    Good job

  24. Tudor Lechintan May 18, 2017 at 9:50 pm - Reply

    The improved constructor:
    public potpie(int d, int m, int y){
    day = ((d > 0 && d <= 31)? d : 1);
    month = ((m > 0 && m <= 12)? m : 1);
    year = y;

    System.out.printf("The constructor is %s", this);
    }

  25. Damian Flynn May 18, 2017 at 9:52 pm - Reply

    I typed toString incorrectly. I typed it ToString.

    it wouldn’t print the date, took me ages to figure it out.

    我好笨。

  26. Salem Alkaabi May 18, 2017 at 9:53 pm - Reply

    👍🏿👍🏿

  27. Liam Lorenzen May 18, 2017 at 9:54 pm - Reply

    Can someone please explain me why we use (this)? I don’t get it:
    System.out.printf("The constructor for this is %sn", this);

    this prints out the same: System.out.printf("The constructor for this is %sn", toString());

    What is the difference?

  28. jachvideos May 18, 2017 at 9:55 pm - Reply

    does ‘this’ would look to a mathod called toInt or toDouble if I use ‘("%i %f",this, this);’ instead ??

  29. Clash with A Square May 18, 2017 at 9:55 pm - Reply

    welcome to your 40 second java toturial

  30. Waqar Hussain May 18, 2017 at 9:57 pm - Reply

    good job

  31. CanadianBlaze34 May 18, 2017 at 9:58 pm - Reply

    40 second tutorial

  32. sargon marqos May 18, 2017 at 9:58 pm - Reply

    thank you please it is too much for beginner I want to know what toSring do in easy way

  33. Viết Quân Nguyễn May 18, 2017 at 10:00 pm - Reply

    getting really confusing right now after 42 tutorials :(((

  34. maggie xu May 18, 2017 at 10:01 pm - Reply

    Thx for sharing. I have a question. Why the method toString can be called automatically?  Why not need to call it like Object.method() ? Thank you!

  35. gay mario May 18, 2017 at 10:02 pm - Reply

    Dude…. potpie…. This is unprecedented…

  36. CallMeEfe May 18, 2017 at 10:03 pm - Reply

    im unfamiliar with the C language (the printf function) and dont really know how the "this" word works. so how is it that "this" references the toString method even though it wasn’t called inside the main method?

  37. S. OUCHENE May 18, 2017 at 10:03 pm - Reply

    I do not recommend your programming style, your class names **must** always start with a capital letter

  38. M 2k May 18, 2017 at 10:07 pm - Reply

    Could someone explain why object’s and constructor’s "this" keyword refer to things from their class and not to things in the class they are being called in, please?

  39. Olivier Sama May 18, 2017 at 10:07 pm - Reply

    i have printed out with an extra %s. 🙁

  40. GeishaTheSerpantClan May 18, 2017 at 10:09 pm - Reply

    why are you mixing C and JAVA??

  41. Abir Mahmood Abir May 18, 2017 at 10:09 pm - Reply

    not very clear , is "this" here referencing to the toString method or what?

  42. Submersed24 May 18, 2017 at 10:09 pm - Reply

    What if you just want to output /n or %s? Its really strange why you include those inside of the parenthesis in System.out.print("")

  43. smittenessmee May 18, 2017 at 10:10 pm - Reply

    So I tried replacing ‘this’ with toString() on System.out.printf("The constructo…..%sn", this);
    and I got the same result. So why exactly are we using ‘this’ ?

  44. Albert Du May 18, 2017 at 10:13 pm - Reply

    is there like a toInt() method or toDouble method?

  45. Ronald Grant May 18, 2017 at 10:13 pm - Reply

    in the Uk its day month year

  46. Raju Benny May 18, 2017 at 10:16 pm - Reply

    Why are you using C language representation to print output. You should teach us the java representation of output.You are making the code a bit complicated and confusing beginners to learn.

  47. Mark Rai May 18, 2017 at 10:16 pm - Reply

    poh’pai!

  48. ken l May 18, 2017 at 10:18 pm - Reply

    What if you had multiple constructors with all of them using the "this" keyword?

  49. Gurman Gill May 18, 2017 at 10:20 pm - Reply

    Who else is watching tutorials @ 1.5 speed .

  50. Username? Whats that? May 18, 2017 at 10:20 pm - Reply

    how does java know ‘toString’ is the class for ‘this’

    how does ti print that?

    is it random? feels like java is guessing most of my stuffs

Leave A Comment

*