Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default initiating objects question(kinda)

    When you make a new class with the main method in it, does it automatically create an instance of that class?

    example

    class TennisShoe

    main
    do stuff

    //since it is doing stuff, is there an object that is created to start the main for doing stuff?
    Last edited by helpisontheway; 01-11-2010 at 02:52 AM.

  2. #2
    DarrenReeder is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Im not sure what you mean.... i THINK you mean..does the main thread automaticly run? if thats your questiion...then yea it does.

    Also if you want to run a 'main' method on a different class when u make a object you use a 'constructor'... So...

    Java Code:
    public class myClass{
       public static void main(String[] args){
          secondClass scObject = new secondClass();
          //This is run When program starts
       }
    }
    
    
    public class secondClass{
    
    public secondClass{ // Constructor
    System.out.println("test");
    //THis will also run
    }
    }
    i hope this answers ur question and i hope wat i said is right... im quite new to java as well as u - WEll Goodluk with it :S

  3. #3
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    The answer is yes. when you execute the "main" method it creates an instance of the class that that is runs in..

  4. #4
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    example..
    Java Code:
    public class Main {
       int index;
       public Main() {
          index=1;
       }
       
       public static void main(String[] args) {
               for (int i=0; i < 10; i++) {
                   index++;
                   System.out.Println(index);
               }       
           }
           
        }
    }
    Last edited by shuks; 01-11-2010 at 03:12 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    //since it is doing stuff, is there an object that is created to start the main for doing stuff?

    No. main() is a static method so there is no instance of the class associated with it. No instance of the class is created or needed: the behaviour of main() is behaviour of the class as a whole.

    If you want an instance of the class you have to create one with new. If you don't create such an instance the main() method will not be able to access fields etc. The code in #4 doesn't compile. To be able to increment the index variable you would have to write:

    Java Code:
    public class Main {
       int index;
       public Main() {
          index = 1;
       }
       
       public static void main(String[] args) {
          Main test = new Main();
          for(int i = 0; i < 10; i++) {
             test.index++;
             System.out.println(test.index);
          }       
       }
    }

Similar Threads

  1. Simple question about objects.
    By shroomiin in forum New To Java
    Replies: 10
    Last Post: 10-10-2009, 03:33 AM
  2. Kinda stuck of learning Java
    By jurka in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 05:00 PM
  3. need help, weird question kinda.
    By carlos123 in forum New To Java
    Replies: 6
    Last Post: 01-22-2008, 04:19 AM
  4. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 05:10 PM
  5. k this is my ultimate project. kinda
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 05:47 AM

Posting Permissions

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