Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

We always write:

public static final int A = 0;

  1. My question is static final the only way to declare a constant in a class?

  2. If I write public final int A=0; instead, Is A still a constant or just an instance field?

  3. What is an instance variable? What is the difference between an instance variable and an instance field?

1 Answer

0 votes
by (13.1k points)
  1. You can use an enum type from Java 5 for the purpose you have described, It is type-safe.

  2. A is an instance variable. Constants just mean the value doesn’t change.

  3. Instance variables are data members belonging to the object and not the class. Instance variable = instance field.

If you are talking about the difference between the instance variable and the class variable, the instance variable exists per object created. While the class variable has only one copy per class loader regardless of the number of objects created.

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...