Модификатор final
Следует также отметить, что к абстрактным классам (с ключевым словом abstract ), нельзя применить модификатор final , т.к. это взаимоисключающие понятия.
public final class String< // . >
Final метод
effectively final
С java 8 появилось понятие — effectively final . Применяется оно только к переменным (в том числе аргументам методов). Суть в том, что не смотря на явное отсутствие ключевого слова final , значение переменной не изменяется после инициализации. Другими словами, к такой переменной можно подставить слово final без ошибки компиляции. effectively final переменные могут быть использованы внутри локальных классов ( Local Inner Classes ), анонимных классов ( Anonymous Inner Classes ), стримах (Stream API).
public void someMethod() < // В примере ниже и a и b - effectively final, тк значения устанавливаютcя однажды: int a = 1; int b; if (a == 2) b = 3; else b = 4; // с НЕ является effectively final, т.к. значение изменяется int c = 10; c++; Stream.of(1, 2).forEach(s->System.out.println(s + a)); //Ок Stream.of(1, 2).forEach(s-> System.out.println(s + c)); //Ошибка компиляции >
Блиц
1. Что можно сказать про массив, когда он объявлен final ?
Т.к. массив – это объект, то final означает, что после присвоения ссылки на объект, уже нельзя ее изменить, но можно изменять состояние объекта.
final int[] array = ; array[0] = 9; //ок, т.к. изменяем содержимое массива – array = new int[5]; //ошибка компиляции
2. Можно ли подменить значение у объекта String (не меняя ссылки на объект)?
Известно, что класс String — immutable , класс объявлен final , значение строки хранится в массиве char , который отмечен ключевым словом final . Да, можно. Ключевой момент – это понимание использования колючего слова final с объектами. Для подмены значения использует ReflectionAPI.
import java.lang.reflect.Field; class B < public static void main(String[] args) throws Exception < String value = "Old value"; System.out.println(value); //Получаем поле value в классе String Field field = value.getClass().getDeclaredField("value"); //Разрешаем изменять его field.setAccessible(true); //Устанавливаем новое значение field.set(value, "JavaRush".toCharArray()); System.out.println(value); /* Вывод: * Old value * JavaRush */ >>
Обратите внимание, что если бы мы попытались изменить подобным образом финальную переменную примитивного типа, то ничего бы не вышло. Предлагаю вам самостоятельно в этом убедить: создать Java класс, например, с final int полем и попробовать изменить его значение через Reflection API.
Java static final field class
Вопрос глуппый но задам. Сборщик мусора не сходит сума при работе с immutable? Наример нам приходится в программе таскать ‘с собой’ масивы строк и паралельно в них менять значения. Это жесть какая нагрузка на железо.
public static void main(String[] args)
Вывод: I love Java I love Java Честно говоря, не понимаю, что удивительного в этом коде? Код же выполняется сверху вниз. А тут четверть статьи этому посвятили) Я так понимаю, что если я в конце в коде напишу: System.out.println(str1);, то вывод будет: I love Java I love Python Или я что-то не так понял?
Ведьмаку заплатите – чеканной монетой, чеканной монетой, во-о-оу Ведьмаку заплатите, зачтется все это вам
Всё что я должен понять из этой статьи: final для класса — класс нельзя наследовать, final для метода — метод нельзя переопределять, final для переменной — нельзя изменять первое присвоенное значение (сразу присваивать не обязательно), имя пишется капсом, слова через нижний пробел. Объекты всех классов обёрток, StackTrace, а также классы, используемые для создания больших чисел BigInteger и BigDecimal неизменяемые. Таким образом, при создании или изменении строки, каждый раз создаётся новый объект. Кратко о String Pool: Строки, указанные в коде литералом, попадают в String Pool (другими словами «Кэш строк»). String Pool создан, чтобы не создавать каждый раз однотипные объекты. Рассмотрим создание двух строковых переменных, которые указаны в коде литералом (без new String).
String test = "literal"; String test2 = "literal";
При создании первой переменной, будет создан объект строка и занесён в String Pool. При создании второй переменной, будет произведён поиск в String Pool. Если такая же строка будет найдена, ссылка на неё будет занесена во вторую переменную. В итоге будет две различных переменных, ссылающихся на один объект.
Мало примеров и в целом, недосказано. Под конец вскользь упомянут String Pool, а что это не объясняется. Статья озаглавлена какFinal & Co, а по факту пару примеров по строкам, ну, такое. Это называется собирались пироги печь, а по факту лепёшки лепим. В любом случае, конечно, спасибо за труд. Но, гораздо лучше про строки написано здесь: Строки в Java (class java.lang.String). Обработка строк в Java. Часть I: String, StringBuffer, StringBuilder (более детальная статья на Хабре).
Получается мы не можем создать поле какого нибудь класса не константой public static final String name = «Амиго»; обязательно только так? => public static final String CHARACTER_NAME = «Амиго»; или можно написать и так и так?
«В прошлых лекциях мы видели простой пример наследования: у нас был родительский класс Animal, и два класса-потомка — Cat и Dog» ?! А была лекция о наследовании?! Может быть я где-то пропустил, поделитесь ссылкой, пожалуйста 🙂
Java Fields
A Java field is a variable inside a class. For instance, in a class representing an employee, the Employee class might contain the following fields:
The corresponding Java class could be defined like this:
Field Declaration Syntax
A Java field is declared using the following syntax:
[access_modifier] [static] [final] type name [= initial value] ;
The square brackets [ ] around some of the keywords mean that this option is optional. Only type and name are required.
First an access modifier can be declared for a Java field. The access modifier determines which object classes that can access the field. In the Employee example above there were no access modifiers.
Second, a data type for the Java field must be assigned. In the Employee example above the data types String , int and Date were used.
Third, the Java field can be declared static . In Java, static fields belongs to the class, not instances of the class. Thus, all instances of any class will access the same static field variable. A non-static field value can be different for every object (instance) of a class.
Fourth, the Java field can be declared final or not. A final field cannot have its value changed. A final field must have an initial value assigned to it, and once set, the value cannot be changed again. A final field is often also declared static . A field declared static and final is also called a «constant».
Fifth, the Java field is given a name. You can choose this name freely, but there are some restrictions on what characters the name can contain.
Sixth, you can optionally set an initial value for the field.
Some of the above options are described in more detail in the following sections.
Java Field Access Modifiers
The Java field access modifier determines whether the field can be accessed by classes other than the the class owning the field. There are four possible access modifiers for Java fields:
The private access modifier means that only code inside the class itself can access this Java field.
The package access modifier means that only code inside the class itself, or other classes in the same package, can access the field. You don’t actually write the package modifier. By leaving out any access modifier, the access modifier defaults to package scope.
The protected access modifier is like the package modifier, except subclasses of the class can also access the field, even if the subclass is not located in the same package.
The public access modifier means that the field can be accessed by all classes in your application.
Here are a few examples of fields declared with access modifiers. The modifiers are in bold.
The above use of Java field access modifiers are for the sake of this example only. You would probably not use all access modifiers in the same class. Most often you use private and protected . For simple, data carrying classes you may declare all fields public .
The Java access modifiers are covered in more detail in my Java access modifiers tutorial.
Static and Non-static Fields
A Java field can be static or non-static.
A static field belongs to the class. Thus, no matter how many objects you create of that class, there will only exist one field located in the class, and the value of that field is the same, no matter from which object it is accessed. Here is a diagram illustrating static fields:
Static Java fields are located in the class, not in the instances of the class. |
You define a static field by using the static keyword in the field declaration, like this:
Static fields are located in the class, so you don’t need an instance of the class to access static fields. You just write the class name in front, like this:
Customer.staticField1 = "value"; System.out.println(Customer.staticField1);
Non-static Java fields, on the other hand, are located in the instances of the class. Each instance of the class can have its own values for these fields. Here is a diagram illustrating non-static fields:
Non-static Java fields are located in the instances of the class. |
You define a non-static Java field simply by leaving out the static keyword. Here is an example:
To access a non-static field you need an instance of the class (an object) on which you can access it. Here is an example:
Customer customer = new Customer(); customer.field1 = "value"; System.out.println(customer.field1);
Final Fields
A Java field can be declared final . A final field cannot have its value changed, once assigned. You declare a field to be final by adding the final keyword to the field declaration. Here is an example:
The value of the field1 field cannot be changed now. That means, that even if the field belongs to objects (class instances), you cannot vary the value of the field from object to object.
When you cannot change the value of a final field anyways, in many cases it makes sense to also declare it static . That way it only exists in the class, not in every object too. Here is an example:
Since static final fields are often used as constants, the naming convention is to write the field name in all uppercase, and to separate the words with underscore _ . Here is a Java static final field example:
Naming Java Fields
The name of a Java field is used to refer to that field from your code. Here is an example:
Customer customer = new Customer(); customer.city = "New York"; System.out.println(customer.city);
The first line creates a new Customer object (an instance of the Customer class), and stores it in a variable called customer . The second line assigns the String value New York to the Customer objects city field. The third line prints out the value of the city field to the console output.
The naming restrictions and naming conventions for fields are the same as for any other type of variable.
Initial Field Value
A Java field can have be given an initial value. This value is assigned to the field when the field is created in the JVM. Static fields are created when the class is loaded. A class is loaded the first time it is referenced in your program. Non-static fields are created when the object owning them are created.
Here is an example of a Java field being declared with an initial value:
Whether you want to initialize your Java fields (and other variables) to an initial value is up to you. I have made it a habit to always initialize my variables to some sensible value, but it is just a habit. It is not necessary to do so.