Results 1 to 5 of 5
- 01-11-2010, 12:58 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
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 01:52 AM.
- 01-11-2010, 02:00 AM #2
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
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...
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 :SJava 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 } }
- 01-11-2010, 02:04 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
The answer is yes. when you execute the "main" method it creates an instance of the class that that is runs in..
- 01-11-2010, 02:06 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 18
- Rep Power
- 0
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 02:12 AM.
- 01-11-2010, 03:07 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
//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
-
Simple question about objects.
By shroomiin in forum New To JavaReplies: 10Last Post: 10-10-2009, 02:33 AM -
Kinda stuck of learning Java
By jurka in forum New To JavaReplies: 2Last Post: 02-14-2009, 04:00 PM -
need help, weird question kinda.
By carlos123 in forum New To JavaReplies: 6Last Post: 01-22-2008, 03:19 AM -
Creating objects question
By sergm in forum New To JavaReplies: 2Last Post: 12-27-2007, 04:10 PM -
k this is my ultimate project. kinda
By jason27131 in forum New To JavaReplies: 2Last Post: 08-03-2007, 04:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks