Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-25-2009, 04:49 AM   PM User | #1
swraman
New Coder

 
Join Date: Jul 2008
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
swraman is an unknown quantity at this point
Java Classes

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:

Code:
    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
swraman is offline   Reply With Quote
Old 06-25-2009, 09:05 PM   PM User | #2
TheShaner
Senior Coder

 
TheShaner's Avatar
 
Join Date: Sep 2005
Location: Orlando, FL
Posts: 1,125
Thanks: 2
Thanked 40 Times in 40 Posts
TheShaner will become famous soon enoughTheShaner will become famous soon enough
Quote:
Originally Posted by swraman View Post
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.

Quote:
Originally Posted by swraman View Post
In Java, a class is defined, and in the class there is a procedure:

Code:
    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.

Quote:
Originally Posted by swraman View Post
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/tutor...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.

Quote:
Originally Posted by swraman View Post
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
TheShaner is offline   Reply With Quote
Old 06-26-2009, 06:49 AM   PM User | #3
swraman
New Coder

 
Join Date: Jul 2008
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
swraman is an unknown quantity at this point
Thanks. It makes a lot more sense now.
swraman is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:34 AM.


Advertisement
Log in to turn off these ads.