File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is mandatory in a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is mandatory in a class?" Watch "What is mandatory in a class?" New topic
Author

What is mandatory in a class?

Tim Hoang
Greenhorn

Joined: Feb 24, 2011
Posts: 26
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

Joined: Jan 10, 2002
Posts: 60053
    
  65

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

Put this into your compiler and see what happens:
Tim Hoang
Greenhorn

Joined: Feb 24, 2011
Posts: 26
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

Joined: Mar 29, 2010
Posts: 9

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

Joined: Feb 24, 2011
Posts: 26
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is mandatory in a class?
 
Similar Threads
constructor
Public Class
PostFix, Prefix
Get more error in the below program