Results 1 to 7 of 7
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Question concerning methods

    Forgive me for asking a question that should probably be obvious but there is something about methods that confuses me. I've noticed that sometimes a method ends with a {} such as

    Java Code:
    class Employee
    
        String name;
        String title;
        double salary;
        boolean married;
        void displayName() {
             System.out.println("Name " + name);
         }
    while at other times a method ends with just a ; such as

    Java Code:
    Employee employee1 = new Employee();
    employee1.displayName();
    So the question is how do you know when to use {} and when to use ;?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    The first is a method definition; everything between the { ... } makes up the method body; the second is a method call or a constructor call (note the 'new' operator).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    What JosAH said, they aren't the same thing.

    In your class file, you're defining the object you want. Everything in between the brackets is what you want to happen when the object you create calls that method. You don't have a limit on how many methods you can have in an object.

    Your Employee employee1 = new Employee(); creates a new Employee() object with the method displayName() with the reference of employee1.

    Does this answer your question?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    creating method ends with {..}
    while calling the method it can end with ;

  5. #5
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Thank you. That makes sense now

  6. #6
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default

    Yes it does. You explained it very well for me. Thank you very much

  7. #7
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

Similar Threads

  1. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 11:10 PM
  2. Head First Java book-Abstract methods question
    By silverglade in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 01:45 AM
  3. 2 methods with same name Question
    By dienesh77 in forum New To Java
    Replies: 4
    Last Post: 03-22-2011, 02:58 PM
  4. Newbie question about Static methods
    By SCS17 in forum New To Java
    Replies: 9
    Last Post: 02-06-2008, 09:03 AM
  5. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •