Results 1 to 11 of 11
Like Tree2Likes
  • 2 Post By Dreaming

Thread: Significance of the main method in a java program

  1. #1
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Significance of the main method in a java program

    From the little I know of Java so far, a Java program must have at least one class with a main method to run. No main, no programm-execution. Unless the class is like servlets which run on servers. Hope I am right so far.

    Now, why do I get an error when I write this?

    Java Code:
    public class Test {	
    		
    	String s = "Hallo";
    
    	if(s.equals("")) {System.out.println("String is empty");}
    
    }
    If I add the main method, the error is gone, meaning eclipse is showing the error because there is no main method.

  2. #2
    Bambi is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: Significance of the main method in a java program

    As far as I have been taught, you can't use an if statement unless it is in a method. The method doesn't necessarily have to be the main method. As far as the main method is concerned, you have to have one in the controlling class in a java program.

    Someone correct me if I have been taught wrong.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Significance of the main method in a java program

    Your error is because you're trying to put program code out naked in a class, when it needs to be wrapped inside of a method, or constructor, or static initializer block, or non-static initializer block. You should read the introduction to Java tutorials as they will explain all of this and more.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Significance of the main method in a java program

    Executable code, excluding variable declaration and declaration-cum-initialization, is valid only within a method, constructor or initializer block.

    You can't put executable code out in the body of the class.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Significance of the main method in a java program

    OK, I created a method void string() and changed the code to this. Please see the comments in the code

    Java Code:
    public class Test {
    	
    		
    	void string() {	
    		
    	String s = "Hallo";
    	if(s.equals("")) {System.out.println("String is empty");}
    	else {
    		System.out.println("String is NOT empty");
    		};
    	
    	}
    
    //After I did this, it then gave me error, no main method, which is understandable because 
    //every java program must have at least one main method. 
    //However, after adding the main for the sake of it, the error went out, but I expected
    //String is NOT empty to be printed out, but it is all blank, why? 
    	
    	public static void main(String[] args) {
    		
    	}
    
    }
    Last edited by Dreaming; 01-29-2012 at 01:54 AM.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Significance of the main method in a java program

    To clarify, you need to have a main method which the program starts at. Without a main method it will have no entrance point. You want your code to begin from the main method, for example create objects, start them doing something etc.

  7. #7
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Significance of the main method in a java program

    OK, but I have a main method, so why is my program not working. If I understood what the other posters said correctly, to execute statements, they must be in a method, that is, any method. So I put the if statement in a void method just to test it out, then after eclipse threw error no main method, added it. The error disappeared but I still don't get String NOT empty printed out, so why?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Significance of the main method in a java program

    You should post your current code and your current error message.

    But more importantly, you appear to be trying to learn coding by trial and error and my experience here with seeing others attempt this is that it just doesn't work. I strongly urge you to learn Java programming the right way from the ground up by going step wise chapter by chapter through a decent intro to Java book. It will speed up your Java education immensely and will prevent you from making many of your current errors.

  9. #9
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Significance of the main method in a java program

    Yeah thanks but this is simply an attempt to understand. That is actually the full code as it is and there are no errors, it is all blank. My expectation was that "String is NOT empty" would have been printed out but I get nothing printed out. So my question is, if what is required is just a method to execute the if code, then why is it not begin executed despite me adding a method, and also including the main method after eclipse threw the error "no main method"?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Significance of the main method in a java program

    Quote Originally Posted by Dreaming View Post
    Yeah thanks but this is simply an attempt to understand. That is actually the full code as it is and there are no errors, it is all blank. My expectation was that "String is NOT empty" would have been printed out but I get nothing printed out. So my question is, if what is required is just a method to execute the if code, then why is it not begin executed despite me adding a method, and also including the main method after eclipse threw the error "no main method"?
    You need to call the string() method from within the main method. Since the string() method is not static, you would need to create an instance of Test and call the method on this instance. But again, this is nothing but rudimentary Java. Go get an intro text book and study this stuff in order and chapter by chapter. This forum is a poor substitute for this and you're only cheating yourself if you don't do this.

  11. #11
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Significance of the main method in a java program

    OK, thanks, I get it now. So you do not need just any method but specifically the main method to call the string() method. Yeah, will definitely get an intro book.
    Fubarable and JosAH like this.

Similar Threads

  1. Java main method (J2SE)
    By myka in forum Advanced Java
    Replies: 2
    Last Post: 03-19-2010, 07:59 PM
  2. java graphics within main method
    By jforce93 in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 10:33 PM
  3. Replies: 1
    Last Post: 08-07-2007, 06:16 AM
  4. significance of static variables and methods
    By imran_khan in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 09:52 AM
  5. The main method in java...
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:21 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
  •