Pratik Kandalgaonkar

Greenhorn
+ Follow
since Jun 28, 2022
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
15
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Pratik Kandalgaonkar

There is a lot of confusion about the difference between constructors and methods. One difference you won't understand is that methods are members of a class and constructors aren't.

  • You can call a method as many times or as few times as you like; you call a constructor once for each object, immediately after that object has been created.
  • Methods always have return types (or void) and constructors never do.



  • A.) Thanks for making the difference clear, Cheers!!

     I shall let you find out what happens if you mistakenly give a constructor a return type.But constructors aren't methods and methods aren't constructors.



    i named it Student_1, because Student already existed....

    When i use,
    public void Student_1(String name, int age)

    The code gives me this error on line 15
    error -> The constructor Student_1(String, int) is undefined

    and i am not able to declare anything, and the code won't accept the constructor



    But, when i declare a normal constructor it works as intended



    Output


    This makes the difference between methods and constructors more clearer, as i can define a constructor just once and methods can be defined indefinitely
    Thanks!! Cheers!!
    1 year ago
    Yeah i got confused about the constructor part too, the instructor said it's not like a method but then she describes it as kind of a method 😅😅
    , But now i am learning the java language from udemy created by tim buchalka, so far its good
    1 year ago

    Campbell Ritchie wrote:We don't have the time to go through every tutorial on YouTube, but I have seen very few that I would describe as good. Most that I can remember miss out so much that they are only of use to experienced people
    Please provide a link to that tutorial, but I can't promise to view it.



    The video in question is in Hindi Language, which is my native language, but the OOP doc provided is written in english, you dont have to watch the video as it's 1hr6min plus in my native language, but rather read the pdf they provide which is 13/15 pages,
    link to the doc, the link will be in the video description, in case mine doesn't work OOP in Java - Google Docs

    here's the link to that video
    Java OOPs in One Shot | Object Oriented Programming
    1 year ago
    @Campbell Ritchie

    Where did you find a description of those concepts?



    There is this video on Youtube, "Java OOP in One Shot" from channel "Apna College", i found the code and the concepts there, the video is in native language, but they have provided the concept sheet down in the description

    Thanks for the insight it's really Helpful
    Thanks a lot for help!!!
    1 year ago
    @Campbell Ritchie i am not that great with my english comprehension, but i will try my best to write better titles;
    1.) i was going through OOP concepts, practicing non-parameterised constructors, but it's an interesting point as to why parameterised constructors are better than the non-parameterised one?
    2.) if i understand correctly, should i keep attributes of my class private?
    Thankyou for the reply
    1 year ago
    Thankyou so much, thats actually a great insight!!
    1 year ago
    @Tim Moores ,

    There used to be a party trick with a static initialiser and System.exit() which ran without a main() method, but it hasn't worked since Java7.


    before Java7 if we use just System.exit() and static initializer, we didn't have to use main() to run our code?, imean if we use static initializer to read our program, why use main()
    btw what is a static initializer exactly?
    1 year ago


    Now, when i don't use ".this" keyword it gives me the same output



    Output for both of them is

    So what's the difference between using the property name directly and using .this with the property name?
    1 year ago
    Sorry for the late reply, was a bit stuck at a place, i actually messed up with the question there, i didn't accidentally post the entire code, and just realized it, i used public static void main() and that was the class i used and called it through the main class, i was confused beyond belief, i am sorry for this guys,
    this was not actually my code, so i was confused and frustrated and didn't think it through, which led to this.
    i'll make sure it won't happen again.....

    @Frank Carver
    @Campbell Ritchie so we didn't have to write public static void main(String[] args), when we used System.exit()?
    1 year ago
    Isn't it a compulsion to use public static void main(String[] args){ }  in every program?, but this still gives me output!

    1 year ago
    Thanks!!

    Norm Radder wrote:

    why does it give me "true"??


    Look at the API doc for the Hashset add method, it says:


    Returns:
    true if this set did not already contain the specified element

    1 year ago
    i've created an Array list, on which my square operation will be performed



    this piece of code prints the Array list for me



    now, when i try to use this piece of code, i get a boolean output of "true", and even if i get a boolean output why does it give me "true"??

    1 year ago
    Thanks for all the replies they are relly helpful!!
    i'll look into JShell, thanks for the recommendation
    1 year ago
    What is the difference between

    just declaring a variable

    assigning variable nothing



    according to my knowledge the latter one assign null
    but i didn't get the former one (String j)
    1 year ago
    after trying multiple indentation styles i came to the conclusion i am more comfortable with this, now which variation of k&R is this idk, but this one is more readable and concise, so i like it, and it's the Default indentation style for Java right??

    1 year ago