Results 1 to 9 of 9
Thread: the main method in java
- 01-10-2013, 04:28 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
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!
- 01-10-2013, 04:40 AM #2
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
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?
- 01-10-2013, 05:11 AM #3
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-10-2013, 05:23 AM #4
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
- 01-10-2013, 06:18 AM #5
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
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.
- 01-10-2013, 06:22 AM #6
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
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? 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!
- 01-10-2013, 06:52 AM #7
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: the main method in java
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.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?
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 } }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:why is string an array?
args.length
and that would tell you how many command-line arguments that there are.
Here is a link to a Java tutorial that talks all about arguments that come from the command-line:...what exactly is an argument?
Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)
- 01-10-2013, 07:25 AM #8
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
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!
- 01-10-2013, 11:40 AM #9
Member
- Join Date
- Jan 2013
- Location
- INDIA
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Significance of the main method in a java program
By Dreaming in forum New To JavaReplies: 10Last Post: 01-29-2012, 03:21 AM -
Why did Java designers make main method as static ?
By makpandian in forum New To JavaReplies: 9Last Post: 11-13-2010, 09:51 PM -
Java main method (J2SE)
By myka in forum Advanced JavaReplies: 2Last Post: 03-19-2010, 07:59 PM -
java graphics within main method
By jforce93 in forum New To JavaReplies: 4Last Post: 02-02-2010, 10:33 PM -
The main method in java...
By lenny in forum New To JavaReplies: 1Last Post: 07-31-2007, 06:21 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks