Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help understanding Objects and Constructers

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help understanding Objects and Constructers

    Hello Java friends,

    I'm struggling to understand objects and classes.

    Can someone please tell me why in the first code snippet for the Rectangle class the object is declared on two separate lines?

    Rectangle myRectangle;

    System.out.print ("Please enter length of rectangle: ");
    rectangleLength = keyboard.nextDouble();

    System.out.print ("Please enter height of rectangle: ");
    rectangleHeight = keyboard.nextDouble();

    //create new Rectangle object (myRectangle)
    myRectangle = new Rectangle(rectangleLength, rectangleHeight);

    i.e Why does the example create a reference to the object Rectangle, then you take in the parameters and then create the object at the end????


    Yet in the second example of the BankAccount class the object is declared on one line? What is the difference?

    BankAccount account1= new BankAccount ("12345,", "Ben Murphy");

    System.out.print("Enter amount the deposit");
    amount = keyboard.nextDouble();
    //object.method.parameter
    account1.deposit(amount);

    System.out.print("Deposit was made");
    System.out.println("Balance = " + account1.getBalance());

    System.out.println();

    System.out.println("Enter amount to withdraw: ");
    amount = keyboard.nextDouble();
    ok = account1.withdraw(amount);


    Are these two ways of doing the exact same thing? Are they both constructors?

    I have watched videos, done example tutorials yet I seem to be struggling with this concept. Many thanks.


  2. #2
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Help understanding Objects and Constructers

    Because the scanner must read in height and length, before they are passed as parameters. The BankAccount Class, on the other hand doesn't require parameters to be read in. There both objects, the constructor would normally be in tester.

  3. The Following User Says Thank You to llowe29 For This Useful Post:

    thedarkhitman (November 14th, 2013)

  4. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Help understanding Objects and Constructers

    get length;
    get height;
    Rectangle myRectangle = new Rectangle(length, height);
    But the variable can be declared after user input. It's just personal preference.
    Improving the world one idiot at a time!

  5. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help understanding Objects and Constructers

    Ahhhhhhh! Damn I can't believe I overlooked that! I have spent days on this before posting in here.

    Thank you llowe29! Much respect to you!

    --- Update ---

    Hey Junky,

    Ah I see. Now I understand my own confusion too. Thank you! How long have you been programming Java? I find it easier to modify code, than write code from scratch.

    Can you offer any tips? I want to get a trainee Java job and I am kinda struggling with some concepts. How long did it take you to learn? What's the secret?

  6. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Help understanding Objects and Constructers

    I have been using Java since 1997 and you never stop learning. I learnt something new the other day about switch statements. The best way to learn is to practice. Read about classes and their methods in the API then write a small test program to see what results you get and if they match your understanding.
    Improving the world one idiot at a time!

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help understanding Objects and Constructers

    Quote Originally Posted by thedarkhitman View Post
    How long have you been programming Java?How long did it take you to learn?
    More years than the average person has been alive. Considering the accelerated growth of computer science starting years ago and continuing on, we are all still learning every day. It is a journey, not a destination

    Quote Originally Posted by thedarkhitman View Post
    I find it easier to modify code, than write code from scratch.
    Can you offer any tips?
    Start writing code from scratch. It is the best way to develop the ability to write code from scratch.

    Quote Originally Posted by thedarkhitman View Post
    I want to get a trainee Java job and I am kinda struggling with some concepts. What's the secret?
    Write code. Write something you can handle. Modify it. Rewrite it. Ask for ideas from friends. Do other people's assignments that are posted on the forum (just don't give them the solution!).

Similar Threads

  1. Need help with understanding arrays
    By mist4lyf in forum Collections and Generics
    Replies: 2
    Last Post: April 7th, 2013, 09:38 AM
  2. Need help understanding Java
    By Jafke104 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2013, 10:52 PM
  3. Understanding Classes and Objects
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2012, 10:35 AM
  4. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  5. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 01:55 PM