-1

There are many discussions about static and final variables in Java. I really want to know the differences between the following declarations. Seems to be confusing

public class foo() {
    private static final int a;
    private static int b;
    private final int c;
    private int d;
    public static final int e;
    public static int f;
    public final int g;
    public int h;
}

Which one can be modified/accessed inside/outside of the class?

P.S: The question in In Java, difference between default, public, protected, and private is a larger scope. Mine is focusing on some confusing points!

Community
  • 1
  • 1
mahmood
  • 23,197
  • 49
  • 147
  • 242

6 Answers6

4

private means it can be accessed only by instances of class foo.

public means it can be accessed from any object owning a reference to an instance of Class foo.

static means it belongs to the class and thus, it's shared by all foo instances.

final means it can't change its initial value.

final properties can't be modified once initialized. static properties can be modified, but remember that the new value is shared by all instances. private properties can be modified only by a foo instance itself.

This means that a static final property that: can't be modified; is shared by all instances.

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84
2
private static final int a; // accessed only         / inside only
private static       int b; // accessed and modified / inside only
private        final int c; // accessed only         / inside only
private              int d; // accessed and modified / inside only
public  static final int e; // accessed only         / inside and outside
public  static       int f; // accessed and modified / inside and outside
public         final int g; // accessed only         / inside and outside
public               int h; // accessed and modified / inside and outside

As you can see:

  • static has no effect here whatsoever
  • final reduces accessed and modified to accessed only
  • private/public determines inside only / inside and outside
barak manos
  • 29,648
  • 10
  • 62
  • 114
  • You probably want to qualify your assertion that "static has no effect here whatsoever" to clarify that you mean it has no effect as far as accessibility is concerned. It does, of course have an effect in other ways. – Klitos Kyriacou Nov 01 '16 at 08:13
  • @KlitosKyriacou: Yes, I did put some thought into that immediately after posting my answer. However, I think that the word `here` implies it, and in addition to that (even more persuasive) - it makes no sense for a keyword to have no effect whatsoever, so I think it's pretty obvious that "no effect" is in the context of the question... What do you think? – barak manos Nov 01 '16 at 08:18
1

public attributes can be accessed from any class.

private attributes can be accessed just in the class where it's declared. (This is why we need to include getters and setters for example in the other classes to retrieve private vars)

final attributes cannot be modified and set to a different value.

static attributes are accessed in the class itself and in its instances.

Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84
Nadim Baraky
  • 459
  • 5
  • 18
0

In context of the class identifiers the statements have meaning as explained below: firstly private and public keywords are access signifiers, which means all members with private keyword are only visible in the scope of the class they are declared. All the members with public keyword are visible outside the scope of the class.

e.g

class foo{
      public int myVar;
      private int myBar;
}

Now if you instantiate `foo'

foo f = new foo();
f.myVar // is valid
f.myBar // is invalid and generates compile time error.

static keyword signifies that the allocation of the identifier is common for all instances of the class, so this identifier is allocated at the time when compiler encounters type definition for first time. Since, the type allocation for any class happens only once, there is only one instance of the static field maintained which can be accessed across all objects of this class.

final keyword signifies (in context of identifiers), that these identifiers can be initialised once and are then closed for any modification. (in context of methods it means that the method cannot be overridden by the derived classes).

Now coming back to your question, following statements will mean:

private static final int a; 
// 'a' has scope local to the class and its value is accessible through Type
// and shared across all instances of that type and its value cannot be
// changed once initialised. 
private static int b;
// 'b' has scope local to the class and its value is accessible through Type
// and shared across all instances of that type.
private final int c;
// 'c' has scope local to the class and its value cannot be changed once 
// initialised.
private int d;
// 'd' has scope local to the class
public static final int e;
// 'e' has scope beyond the class, it can be accessed outside the class 
// through an instance of this class and its value is accessible through
// Type and shared across all instances of that type 
// and its value cannot be changed once initialised. 
public static int f;
// 'f' has scope beyond the class, it can be accessed outside the class and   
// value is accessible through Type and shared across all instances of that
// type
public final int g;
// 'g' has scope beyond the class, it can be accessed outside the class
// through an instance of this class 
// and its value cannot be changed once initialised.
public int h;
// 'h' has scope beyond the class, it can be accessed outside the class
// through an instance of this class

Cheers!

akka16
  • 83
  • 1
  • 6
0

public- A class, member method, constructor, interface etc declared public can be accessed from any other class.we can say that it have global access.

private-Member Methods, memeber Variables and Constructors that are declared private can only be accessed within the declared class itself.Variables that are declared private can be accessed outside the class if public getter methods are present in the class.

final- final will ensure that the field is a constant and can't be changed

static-it is associated with the type and not with the instances. i.e. only one copy of the field will be present for all the objects and not individual copy for each object.means a single copy of field will be shared among all the object of that class

static final-all instances of that class will share the same value and can't be modified after first initialization.

Zia
  • 1,001
  • 1
  • 13
  • 25
-2
  1. private means it can not be seen outside of class.
  2. static means that the variable is associated with the class, not the object. This means that for any object, its static variables will be shared by all objects of the same class. (more information)
  3. final means that once you assign a value to the variable, you cannot reassign it.

From outside the class you can only access public variables, but you can't modify the final ones.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103