Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is mandatory in a class?

 
Tim Hoang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that a class must have at least one main method, but what else? Constructors? Interfaces?

When should I ever need to use a method's ( ) like for example:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Hoang wrote:I know that a class must have at least one main method

Not correct. Only a class that you want to be the entry point of a program needs main.

but what else? Constructors? Interfaces?

Nothing.

When should I ever need to use a method's ( ) like for example:

When you have something that you want a method to do.
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put this into your compiler and see what happens:
 
Tim Hoang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When should I ever need to use a method's ( ) like for example:

When you have something that you want a method to do.


With that said, isn't it also possible to not use it at all? I have to hand write objects, so I would like to correct myself beforehand.
 
Deep Purohit
Greenhorn
Posts: 9
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With that said, isn't it also possible to not use it at all? I have to hand write objects, so I would like to correct myself beforehand.

Yes its not necessary to use method.

- A method generally defines the subpart of the functionality what you are trying to achieve by writing a class.
- In method you can write the 'code' which is going to get reused.

Lets say you have a class Horse, it has code to "walk" written inside a method walk().
So every time you have to make the Horse walk you will not write the code to make it 'walk' instead you will call the method walk() where the code is already written.
 
Tim Hoang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that helped. It now leaves me to my last question what is the order or how do I properly use dot operators for any occasion. For some reason this is not getting into my brain. (classes,method,objects)

object.method() ???
object.method.class ???
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dot is to access a member of a class (a member variable or a method).

object.method() means: call the method named "method" on the object that the variable named "object" refers to.

Note that "class" is a special property of objects that returns the java.lang.Class object of an object, which contains information about the class that the object belongs to.

object.method.class means: get the java.lang.Class of the member variable named "method" of the object that the variable named "object" refers to. (Note that this line of code will not work if "method" is actually a method - it will only work if "method" is a member variable).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic