Can Java have same name variable

September 3, 2020 · Team EasyExamNotes · 1 min read (est.) · 0 comments

Share on WhatsApp

Why Java not having same name variables ?
Ans.

As a computer faculty, I found following reason during practise;
Consider this example:

class EEN {

public static void main(String args[]) {

int b = 10;

String b = “easyexamnotes.com”;

System.out.println(b);

} 

}

In above Java program example, there are two variables with name b, one is having type int, another is having type String with different values.

When I am printing value of b, Java compiler will get confuse, which variable b to be print.

Because there is no format specifier (%d,%c,%f) in Java like C programming.

That is the reason Java programming not having same name of variables.

Discussion

Ask a question, share your thoughts or report an error.

Sign in to participate in the discussion.

Join the discussion