Results 1 to 7 of 7
Thread: Question concerning methods
- 05-17-2011, 08:52 PM #1
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
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
while at other times a method ends with just a ; such asJava Code:class Employee String name; String title; double salary; boolean married; void displayName() { System.out.println("Name " + name); }
So the question is how do you know when to use {} and when to use ;?Java Code:Employee employee1 = new Employee(); employee1.displayName();
- 05-17-2011, 08:58 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-18-2011, 06:30 AM #3
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.
- 05-18-2011, 07:25 AM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
creating method ends with {..}
while calling the method it can end with ;
- 05-18-2011, 02:44 PM #5
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Thank you. That makes sense now
- 05-18-2011, 02:45 PM #6
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Yes it does. You explained it very well for me. Thank you very much
- 05-18-2011, 02:46 PM #7
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
Similar Threads
-
Static and non static class methods question
By silverglade in forum New To JavaReplies: 2Last Post: 05-14-2011, 10:10 PM -
Head First Java book-Abstract methods question
By silverglade in forum New To JavaReplies: 4Last Post: 04-30-2011, 12:45 AM -
2 methods with same name Question
By dienesh77 in forum New To JavaReplies: 4Last Post: 03-22-2011, 01:58 PM -
Newbie question about Static methods
By SCS17 in forum New To JavaReplies: 9Last Post: 02-06-2008, 08:03 AM -
question about abstract methods
By osval in forum New To JavaReplies: 1Last Post: 07-25-2007, 08:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks