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?
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
Re: the main method in java
Quote:
Originally Posted by
DarrylBurke
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.
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.
Re: the main method in java
Quote:
Originally Posted by
kaydell2
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!
Re: the main method in java
Quote:
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
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
}
}
Quote:
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.
Quote:
...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)
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!
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.