Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between declaring variable out of main method and inside main method?

Tags:

java

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.

like image 624
Brave Satisfaction Avatar asked Jul 17 '13 12:07

Brave Satisfaction


People also ask

Can you declare variables outside the main method?

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.

Can you declare variables outside main method in Java?

We can declare class variables anywhere in class, but outside methods.

Can we declare variable in main method?

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.

Can we declare variable inside main method in Java?

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().


2 Answers

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"
   }
}
like image 190
Mohamed Amine Avatar answered Oct 02 '22 18:10

Mohamed Amine


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

like image 41
Benjamin Schwalb Avatar answered Oct 02 '22 19:10

Benjamin Schwalb