Results 1 to 8 of 8

Thread: Object

  1. #1
    mayankt is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Post Object

    Do We have objects for main class? Yes or NO. If Yes then how we can access the variables and methods of the main class and where they declared?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all think about that, from where your execution start? What the main class (or main method) do?

  3. #3
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    It doesn't exist a main class, but a main method.

    You can place the main method in any class you have. For example, if we have a class called Person:

    Java Code:
    public class Person{
    	private String name;
    	private int age;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    }
    We can create the main method inside this class (or inside any class).

    Java Code:
    public class Person{
    	private String name;
    	private int age;
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	
    	public static void main(String[] args) {
    		
    	}
    }
    If we do it, no object is created once main is static.
    Please don't laugh at my English... I'm trying my best! :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mayankt View Post
    Do We have objects for main class? Yes or NO. If Yes then how we can access the variables and methods of the main class and where they declared?
    No objects are automagically created when the JVM calls your main method. It al depends on the type, access rights and being static or not of variables and methods whether or not they are accessible by code in your main method.

    kind regards,

    Jos

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Much better explanation.

    Quote Originally Posted by renamed View Post
    It doesn't exist a main class, but a main method.
    Basically we called, actually I practice, to refer as the main class which contain the main method. JosAH what you think of that. That's not a standard as far as I know either.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    Much better explanation.



    Basically we called, actually I practice, to refer as the main class which contain the main method. JosAH what you think of that. That's not a standard as far as I know either.
    Think of what?

    kind regards,

    Jos

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Sorry if you are not clear with what I said. In simple words, main class is the class which contain a main method. What's your comment on that?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    Sorry if you are not clear with what I said. In simple words, main class is the class which contain a main method. What's your comment on that?
    Mwah, every class that contains a main( ... ) method (with the correct signature) can be a 'main' class; I don't think much of it.

    kind regards,

    Jos

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  3. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  4. Replies: 1
    Last Post: 04-17-2008, 10:10 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

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