PDA

View Full Version : Java Classes


swraman
06-25-2009, 05:49 AM
Hi

Im new to Java. I have a question about classes in Java.

Im somewhat farmiliar with classes in PHP, and in PHP a class is defined, but it sits there and does nothing until a variable is declared to be an instance of that class.

In Java, this isnt the case, is it? In Java, a class is defined, and in the class there is a procedure:


public static void main (String [ ] args) {


which is executed when the program is run. Is this understanding correct?

This "public static void main(){}" is IN the class definition. So, isnt there a problem that when a new instance of the class is called in "public statc void main(){}" the instance of the class will have the main(){} method in it, and wont the main method in the newly created instance try to execute amain(){} again, and thus run into an infinite procedure?

Also, in java a constructor class is a method in the class that has the same name as the class, correct? and it is run at the time an instance of the class is created?

thanks

raman

TheShaner
06-25-2009, 10:05 PM
Im somewhat farmiliar with classes in PHP, and in PHP a class is defined, but it sits there and does nothing until a variable is declared to be an instance of that class.

In Java, this isnt the case, is it?
Yes, it is. Object-oriented programming is the same concept in any OO language. You instantiate an instance of that class, allowing you to utilize its instance variables and methods.


In Java, a class is defined, and in the class there is a procedure:


public static void main (String [ ] args) {


which is executed when the program is run. Is this understanding correct?

The main method is a special class method that the Java runtime looks for in the class being executed to start the program. Your other classes will not contain this method.


This "public static void main(){}" is IN the class definition. So, isnt there a problem that when a new instance of the class is called in "public statc void main(){}" the instance of the class will have the main(){} method in it, and wont the main method in the newly created instance try to execute amain(){} again, and thus run into an infinite procedure?
You should read up on class methods and variables here: http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

Anything declared with the static keyword is NOT part of the instance of a class, so you will not have the problem you're describing.


Also, in java a constructor class is a method in the class that has the same name as the class, correct? and it is run at the time an instance of the class is created?
No, it is not a method; it's a constructor. Yes, it has the same name, but it does not specify a return type like a method does. When you create an instance of the class, it calls the constructor to initialize the instance variables within the class and possibly run certain instance methods.

I strongly suggest going through Sun's Java documentation found in the link above. It explains objects and classes very well.

-Shane

swraman
06-26-2009, 07:49 AM
Thanks. It makes a lot more sense now.