• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is mandatory in a class?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic