Why can I declare a static field again inside the main() successfully in Java?

why can't i declare a static variable inside main() method in java
can we declare a static variable inside a method in java
why local variables cannot be static in java
static method in java
static keyword in java
when do we declare a member of a class static in java
can we initialize static variable in java
variables inside static methods java

I declared a private static double fd and then I declared double fd again inside the main(). Why can I compile and run it successfully?

    public class HelloWorld {
    private static double fd = 1.0;

    public static void main(String[] args){
        System.out.println(fd); //1.0
        double fd = 2.0;
        System.out.println(fd); //2.0


    }
}

This is not a syntax error (although it will probably result in a logic error, a bug). The compiler will compile this code without complaint. The second declaration of double fd creates a local variable for the main method. The scope of this variable starts with its declaration and ends at the end of the block (as with all local variables). So the next statement uses the local variable, not the instance variable.

The local variable will no longer hold a value after the method has returned. The instance variable will not have been changed.

Hint: Think of statements as looking "upward" from their own location to find each of their variables. They can look outside of their "glass box" in any direction if they fail to find a variable inside their own method.

It is almost always a mistake to use the same name for an instance variable and for a local variable. But it is not a syntax error, so the compiler will not warn you.

Why can't I declare a static variable inside main() method in Java , I would like to share with you the difference between STATIC FIELDS and FINAL VARIABLES (You can declare a FINAL VARIABLE inside a method But you� There is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword. Example

From JLS Scope of a Declaration section:

The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name, provided it is not shadowed.

From JLS Shadowing section:

Some declarations may be shadowed in part of their scope by another declaration of the same name, in which case a simple name cannot be used to refer to the declared entity.

This means that you cannot use simple name (df) to refer class level df variable, because of it's shadowed by local df variable. But there are still two variables and you can use static variable with class name:

public static void main(String[] args){
    System.out.println(fd); //1.0

    double fd = 2.0;

    System.out.println(fd); //2.0
    System.out.println(HelloWorld.fd);
}

Java static: 10 things you need to know about the static modifier, If you don't declare such a block, Java collects all static fields into a single list and initializes them when the class is loaded. A static block� Why Main method is declared public static void in Java is one of Classical interview questions in Java. Every Java Programmer knows that main method is entry point in Java program but only few knows the secret behind signature and syntax of main method in Java. most of Java developer don't really know why main is static , public or void in java

The variable fd is function scoped, think it like, the compiler first check the scope nearest to it, and then if it dosent find there it tries to check the global scope, if it found something in local scope, it will print that, or else moves on to check other scope.

Writing Methods, When we call a method, Java first evaluates each argument (each can be a simple and i; methods can declare and use local variables along with their parameters), Prompt; public class Application { public static void main(String[] args) { int Again, observe that inside this class, we refer to each member by just its name. Answered February 23, 2015. The basic rule is"static variables cannot be declared within any method". the reason being, whenever you declare static variables they are stored in class memory, where as local variables (variables within method) will be stored in individual method stack.

Chapter 12. Execution, will typically start a Java Virtual Machine by invoking method main of class it into the run-time state of the Java Virtual Machine, so that it can be executed. A static field declared by T is used and the field is not a constant variable (�4.12.4). access the registers instead of the object, and never access the object again. A static field gets memory only once for the whole class no matter how many objects of a class are created. To declare a static field, prefix the field declaration in the class with the static modifier.

Can we declare a static variable within a method in java?, Static variables in methods i.e. you cannot use a local variable outside the current method which contradicts with the definition of class/static variable. Therefore, declaring a static variable inside a method makes no sense, if you still try to do so, a compile time error will be generated. Static constants are exactly like static fields except that their values cannot be changed. In the field declaration, the final and static modifiers are both used. For example, perhaps the Item class should impose a restriction on the length of the itemName.

Static vs. Non-Static Methods in Java, In Java, a static method belongs to the class. product is not declared as static, and we are trying to access it from a static main method. In Java, you can use initializer blocks to initialize instance variables. Initializer blocks aren’t executed until an instance of a class is created, so you can’t count on them to initialize static fields. After all, you might access a static field before you create an instance of a class. Java provides a feature called a […]

Comments
  • you are not. you are declaring a local variable which happens to have the same name as your static variable
  • And just for fun and confusion, you can declare the local fd with a different type to the static fd. And for even more fun, you can use fd in its own initializer: double fd = fd = 2.0;.
  • The answer is: because the Java designers chose to make that possible.
  • @JBNizet, now that you've brought it up, I see you are right. I believe it is a legacy from C/C++ but they could have just easily decided to make this a compiler error. So , given the questioned usefulness vs error prone-ability of this "feature", why indeed was it decided to design the specs in this way?
  • Because it's extremely useful, for example to be able to do things like this.name = name.
  • like @JB Nizet wrote: it's a design decision by the creators of the language. the compiler could have produce an error because of this. and given the questioned usefulness vs error prone-ability of this "feature", may it should have?