Results 1 to 9 of 9
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default the main method in java

    Guys I have looked all over the internet, and cannot understand any of these questions: I would really appreciate any help on them. First, and most important thing I would like to know is, what is static: what is a static int, or a static main method? Why is this necessary, and what does it do? Could you please provide an example? Also, why is String[] args necessary within the parameters of the main method? Is this so that it can execute it? Last thing is: objects. Where can I learn about them? I know they are made with the new operator, but don't understand their use? Could you explain why they are useful? Can you give them values? and how do they relate to the dot operator? After these are answered, I will be so happy because I will feel like I actually know a little bit. Right now, I'm just confused... Thank you so, so much in advance!

  2. #2
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: the main method in java

    guys, I have heard on tutorials that static variables within a class are all shared...? I do not know what this means, while static variables are not?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default Re: the main method in java

    Same answer as for most of your earlier threads: The Java™ Tutorials

    A forum isn't a substitute for tutorials.

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

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: the main method in java

    Quote Originally Posted by DarrylBurke View Post
    Same answer as for most of your earlier threads: The Java™ Tutorials

    A forum isn't a substitute for tutorials.

    db
    I have already looked through that. I still do not understand it, which is why I came here.

  5. #5
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: the main method in java

    Well, the declaration for the main() method just has to be that way. It's always the same.

    public static void main(String[] args) {
    }

    "public" means that this method can be called from outside of this class

    "static" means that you can call this method without first having and instance of this class. Notice that it's the main() method that is "static" not a variable. In both cases, it means that you can use them without having an instance of the class, but they mean a little bit different things because a static method isn't exactly the same thing as a static variable.

    "main" is the name of the method.

    What's in between the parentheses is the parameter list. In this case, main() takes a single parameter which is an array of Strings and this array is called "args". This just always has to be there for this main() method to be callable from the outside world. What args is used for are command-line arguments. For example, you can make Java programs that can be run from the command-line and if you pass any command-line args, this is where they come in.

    So that should answer your questions about why main() is always declared with all of those things in the declaration.

  6. #6
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: the main method in java

    Quote Originally Posted by kaydell2 View Post
    Well, the declaration for the main() method just has to be that way. It's always the same.

    public static void main(String[] args) {
    }

    "public" means that this method can be called from outside of this class

    "static" means that you can call this method without first having and instance of this class. Notice that it's the main() method that is "static" not a variable. In both cases, it means that you can use them without having an instance of the class, but they mean a little bit different things because a static method isn't exactly the same thing as a static variable.

    "main" is the name of the method.

    What's in between the parentheses is the parameter list. In this case, main() takes a single parameter which is an array of Strings and this array is called "args". This just always has to be there for this main() method to be callable from the outside world. What args is used for are command-line arguments. For example, you can make Java programs that can be run from the command-line and if you pass any command-line args, this is where they come in.

    So that should answer your questions about why main() is always declared with all of those things in the declaration.
    The only thing I still don't understand is static lol. I'm sorry, but what exactly does that mean, that you can use it without having an instance of the class? So is it better to make everything static? Could you please give me an example? Thank you so much! Also, why is string an array? I get that arguments let you declare variables and that, but what exactly is an argument? Those two things i don't get :(. Thanks kaydell!

  7. #7
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: the main method in java

    The only thing I still don't understand is static lol. I'm sorry, but what exactly does that mean, that you can use it without having an instance of the class? So is it better to make everything static? Could you please give me an example?
    OK, well here is an example. No, you don't usually make things static. The opposite is true, generally, you want to use instance variables so that every instance of a class, that is every object has its own values. That way you can have more than one of them and they'll all still work independently from one another.

    Here's link to a tutorial on classes and objects. It's simpler than some tutorials. Why don't you give it a look?

    Java - Objects and Classes

    Java Code:
    public class StaticDemo {
    	private static String staticVariable = "This is a static value";
    	private String instanceVariable = "This is the value of an instance variable";
    	
    	private static void staticMethod() {
    		System.out.println("You don't need an instance of this class to call this method.");
    		// you can only access static variables in this static context
    		System.out.println(staticVariable);
    	}
    	
    	private void instanceMethod() {
    		System.out.println("You *do* need an instance of this class to call this method.");
    		// you can access both static variables and instance variables in this context
    		System.out.println(staticVariable);
    		System.out.println(instanceVariable);
    	}
    	
    	public static void main(String[] args) {
    		// an example of calling a static method.  We don't need an instance of this class to do this
    		staticMethod();
    		// an example of calling an instance method.  We *do* need an instance of this class to do this.
    		StaticDemo staticDemo = new StaticDemo(); // create an object
    		staticDemo.instanceMethod(); // call the instance method with an instance of this class
    	}
    
    }
    why is string an array?
    Command-line arguments are an array of Strings because there can be more than one of them. To get how many there are you could use:

    args.length

    and that would tell you how many command-line arguments that there are.

    ...what exactly is an argument?
    Here is a link to a Java tutorial that talks all about arguments that come from the command-line:

    Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)

  8. #8
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: the main method in java

    OMG I completely get it now, and also get why main method is static, so that it does not need to use the dot operator. TYSM!

  9. #9
    viren2727in is offline Member
    Join Date
    Jan 2013
    Location
    INDIA
    Posts
    18
    Rep Power
    0

    Default Re: the main method in java

    One Liner answer for "STATIC" = static means ONE per CLASS. static variables and static methods are allocated memory only once for per CLASS. regardless of any number of instances are created. all instances use same memory area.

Similar Threads

  1. Significance of the main method in a java program
    By Dreaming in forum New To Java
    Replies: 10
    Last Post: 01-29-2012, 03:21 AM
  2. Why did Java designers make main method as static ?
    By makpandian in forum New To Java
    Replies: 9
    Last Post: 11-13-2010, 09:51 PM
  3. Java main method (J2SE)
    By myka in forum Advanced Java
    Replies: 2
    Last Post: 03-19-2010, 07:59 PM
  4. java graphics within main method
    By jforce93 in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 10:33 PM
  5. The main method in java...
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:21 AM

Tags for this Thread

Posting Permissions

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