When I was reading book about Java , I saw one example written like this. And I am wondering can I declare variable in outside of main method ? What is difference between declaring variable outside and inside main method? what is " static" 's role in here ? Please some one explain to me? I am new in java.
public class Printstuff {
static int an_integer = 0;
public static void main(String[] args) {
int an_integer = 2;
String[] some_strings = {"Shoes", "Suit", "Tie" };
an_integer = an_integer - 1;
some_strings[an_integer] = some_strings[an_integer] +"+++";
for (int i = 0; i < some_strings.length; i++)
System.out.println(some_strings[Printstuff.an_integer]);
}
}
Best regards.
Instance variables are non-static variables and are declared in a class outside of any method, constructor, or block. As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
We can declare class variables anywhere in class, but outside methods.
We cannot declare static variables in the main method or any kind of method of the class. static variables must be declared like a class member in the class. Because during compilation time JVM binds static variables to the class level that means they have to declare like we declare class members in the class.
Instance Variables Can Be Used in Any Method in a ClassIf you declare num inside the main() method body, it can only be used inside of main().
Declaring a variable in the main method will make it available only in the main. Declaring a variable outside will make it available to all the methods of the class, including the main method.
Example :
public class Foo {
private String varOne = "Test";
public void testOne() {
System.out.println(varOne);
System.out.println(varTwo); // Error, this variable is available in the testTwo method only
}
public void testTwo() {
String varTwo = "Bar";
System.out.println(varOne); // Will display "Test"
System.out.println(varTwo); // Will display "Bar"
}
}
1) Inside vs Outside:
If you declare your object inside a method, it will be visible only in this method. Basically, if you put brackets around it, it's only visible/accessible from within these brackets.
If you declare your object outside the method (inside the class), it depends on the access modifier.
By default, it's visible/accessible from within that class and the whole package.
2) Static
Static means, that static methods / variables belongs to the class itself, and not to its objects (instances of that class).
Example:
public class Members {
static int memberCount;
public Members() {
memberCount++;
}
}
memberCount
exists only once, no matter how many Objects of the class exists. (even before any object is created!)
Every time you create a new Object of Members
, memberCount
is increased. Now you can access it like this: Members.memberCount
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With