What is a protected variable in Java


8.2 Modifier



In this chapter, examples were shown at various points in which keywords such as or in conjunction with certain program elements were used. With the help of these attributes, the properties of classes, methods and variables can be changed. In particular, they have an influence on the lifespan, visibility and Changeability these program elements and are mostly used as Modifier designated. Let us now look at them in context and explain how they work on the various elements of a Java program.

8.2.1 Visibility

The fact mentioned at the beginning that all properties of the base class are taken over in a derived class is not entirely correct in all cases. Although it always has all the variables and methods of the base class, it may not be able to access them if its visibility has been restricted.

The visibility of variables and methods is controlled with the help of the following modifiers:

  • Elements of the type are visible in the class itself (i.e. in its methods), in methods of derived classes and for the caller of instances of the class.
  • Elements of the type are visible in the class itself and in methods of derived classes. In addition, classes of the same package can call them.
  • Elements of the type are only visible in the class itself. Variables remain hidden for derived classes and for callers of instances.
  • Elements that were declared without one of the three mentioned modifiers are shown as package scoped or elements with Standard visibility designated. They are only visible within the package to which this class belongs. In other packages, however, they are invisible.

8.2.2 The attributes at a glance

In the following we want to summarize the most important attributes again and describe their respective effects on the visibility, lifespan or changeability of variables, methods and classes.

private

Methods or variables of the type are only visible in the current class, but remain invisible in all other classes.

Surprisingly, this restriction does not mean that the methods of a class can only access the private member variables of their own object. Rather, it is also possible (quasi from the outside) to access the variables of one other To access the object. Assuming it is an instance of the same class.

The following example program demonstrates this with the help of the class, which has a private member variable. From the implementation of we can see how access to foreign objects of the same type is possible:

001 / * Listing0806.java * / 002 003 publicclass Listing0806 004 {005 publicstaticvoid main (String [] args) 006 {007 ClassWithPrivateA a1 = new ClassWithPrivateA (7); 008 ClassWithPrivateA a2 = new ClassWithPrivateA (11); 009 a2.setOtherA (a1, 999); 010 System.out.println ("a1 =" + a1.toString ()); 011 System.out.println ("a2 =" + a2.toString ()); 012} 013} 014 015 class ClassWithPrivateA 016 {017 privateint a; 018 019 public ClassWithPrivateA (int a) 020 {021 this.a = a; 022} 023 024 publicvoid setOtherA (ClassWithPrivateA other, int newvalue) 025 {026 other.a = newvalue; 027} 028 029 public String toString () 030 {031 return "" + a; 032} 033}Listing0806.java
Listing 8.6: Access to foreign private member variables

From the output of the program you can see that the object a2 to private member variables of the object a1 was accessed: a1 = 999 a2 = 11

protected

Methods or variables of the type are visible in the current class and in derived classes. They are also visible to methods of other classes within the same package. However, they are not visible to callers of the class defined in other packages.

public

Member variables and methods of the type are visible everywhere during their lifetime. They can therefore be used in their own class and by any methods of other classes. The attribute is also important for the class definition itself, because only classes that have been declared as are visible outside of the package in which they were defined. Only one class with the attribute may be created in each source file.

Standard (package scoped)

Classes, methods, variables with standard visibility are only visible within the package in which they were defined. They are useful, for example, to implement generally accessible auxiliary classes in more complex packages that should remain invisible outside the package. Sometimes they can be useful to prevent items from being declared as.

static

Variables and methods with the attribute are not tied to the existence of a specific object; The attribute affects the lifespan of member variables and allows methods to be called without the caller having an object of the class in which the method was defined.

If the attribute is not used, variables within a class are always linked to a specific instance. Their lifespan begins when the object is created and a constructor is called and ends when the object is released by the garbage collector.

final

Member variables with the attribute must not be changed, so they are as Constants to watch. Methods of the type must not be overlaid; nor can classes of the type be used to derive new classes. If, on the other hand, the attribute is not used, member variables can be changed, methods can be overlaid and classes can be derived.

If a method or class has the attribute, the compiler can dispense with the dynamic method search. -Methods can therefore be called more efficiently than normal methods. This is one of the reasons that the Java designers have declared some of the included classes as. At the same time, however, it means that the corresponding classes can no longer be expanded. A prominent example from the runtime library is the class declared as.

Since JDK 1.1, the attribute can also be used on parameters of methods and local variables. In this way the compiler ensures that the variable or the parameter is no longer changed after initialization. The initialization does not necessarily have to take place during the declaration, but can also be carried out later. It is important that a value is assigned exactly once.

transient

Member variables can be assigned the attribute to indicate that they do not have a persistent form. They are ignored when serializing and deserializing objects of this type. Details are described in Chapter 41.

volatile

The keyword is used to indicate that member variables asynchronous, i.e. outside of the current thread, can be modified. The value of a variable declared in this way is therefore read again each time it is accessed (instead of possibly being taken directly from a register in the virtual machine). The use of is rather uncommon. It can be used, for example, to ensure data integrity during multithreading, or it is used to keep access to asynchronously variable memory locations (such as a real-time clock accessed via a variable) up to date at all times.