Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Java Classes

  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    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

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks. It makes a lot more sense now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •