Java static class return this

Java static keyword

In this article we will talk about the static keyword in Java. We cover static variables, methods, nested classes, blocks, and imports.

Java static keyword definition

The static keyword is a non-access modifier. The type that has static modifier does not belong to the instance of a class; it belongs to the class. In addition to this, static can be used to create class initializers, constants, and have imports of static variables without class qualification.

Usage of static keyword

Java static variable

Static variables are also known as class variables. All instances of a class share the same copy of a static variable. They are initialized only once, at the start of the execution. A class variable can be accessed directly by the class name, without the need to create a instance. One common use of static is to create a constant value that is attached to a class.

Static variable example

package com.zetcode; import java.util.ArrayList; import java.util.List; class Being < public static int count; >class Cat extends Being < public Cat() < count++; >> class Dog extends Being < public Dog() < count++; >> class Donkey extends Being < public Donkey() < count++; >> public class JavaStaticVariable < public static void main(String[] args) < Listbeings = new ArrayList<>(); beings.add(new Cat()); beings.add(new Cat()); beings.add(new Cat()); beings.add(new Dog()); beings.add(new Donkey()); int nOfBeings = Being.count; System.out.format("There are %d beings %n", nOfBeings); > >

In the code example, we keep track of beings created with a static variable.

Читайте также:  Популярные модули для python

A static variable is defined. The variable belongs to the Being class and is shared by all instances of Being , including descendants.

The Cat class inherits from Being . It increments the count variable.

The Dog class increments the same class variable. So Dog and Cat refer to the same class variable.

int nOfBeings = Being.count;

We get the number of all beings created. We refer to the class variable by its class name followed by the dot operator and the variable name.

Java static variable properties

  • Static variables have default values.
  • Static variables can be accessed directly in static and non-static methods.
  • Static variables are called class variables or static fields.
  • Static variables are associated with the class, rather than with any object.

Java static method

Static methods are called without an instance of the object. To call a static method, we use the name of the class, the dot operator, and the name of the method. Static methods can only work with static variables. Static methods are often used to represent data or calculations that do not change in response to object state. For instance, java.lang.Math contains static methods for various calculations.

We use the static keyword to declare a static method. When no static modifier is present, the method is said to be an instance method.

Static method restrictions

Static method can only call other static methods. They can only access static data and cannot refer to this and super .

Static method example

package com.zetcode; class Basic < static int public static void showInfo() < System.out.println("This is Basic class"); System.out.format("The Id is: %d%n", id); >> public class JavaStaticMethod < public static void main(String[] args) < Basic.showInfo(); >>

In our code example, we define a static ShowInfo method.

static int >A static method can only work with static variables. Static variables are not available to instance methods.

public static void showInfo()

This is our static ShowInfo method. It works with a static id member.

To invoke a static method, we do not need an object instance. We call the method by using the name of the class and the dot operator.

Java static main method

In Java console and GUI applications, the entry point has the following singnature:

public static void main(String[] args)

By declaring the main method static , it can be invoked by the runtime engine without having to create an instance of the main class. Since the primary reason of main is to bootstrap the application, there is no need to have an instance of the main class.

In addition, if the main method was not static , it would require additional contracts such as a default constructor or a requirement of the main class not to be abstract. So having a static main method is a less complex solution.

Java static block

A code block with the static modifier is called a class initializer. A code block without the static modifier is an instance initializer. Class initializers are executed in the order they are defined, top down, when the class is loaded.

A static block executes once in the life cycle of any program, and there is no other way to invoke it.

Static block example

package com.zetcode; public class JavaStaticBlock < private static int i; static < System.out.println("Class initializer called"); i = 6; >public static void main(String[] args) < System.out.println(i); >>

This is an example of a static initializer.

In the static initializer, we print a message to the console and initialize a static variable.

Java static nested classes

A static nested class is a nested class that can be created without the instance of the enclosing class. It has access to the static variables and methods of the enclosing class.

Static nested classes can logically group classes that are only used in one place. They increase encapsulation and provide more readable and maintainable code.

Static nested classes restrictions

Static nested classes cannot invoke non-static methods or access non-static fields of an instance of the enclosing class.

Static nested class example

package com.zetcode; public class JavaStaticNestedClass < private static int x = 5; static class Nested < @Override public String toString() < return "This is a static nested class; x:" + x; >> public static void main(String[] args) < JavaStaticNestedClass.Nested sn = new JavaStaticNestedClass.Nested(); System.out.println(sn); >>

The example presents a static nested class.

This is a private static variable of the JavaStaticNestedClass class. It can be accessed by a static nested class.

A static nested class is defined. It has one method which prints a message and refers to the static x variable.

JavaStaticNestedClass.Nested sn = new JavaStaticNestedClass.Nested();

The dot operator is used to refer to the nested class.

Java static import

Static import is a feature introduced in Java 5 that allows members (fields and methods) defined in a class as public static to be used in Java code without specifying the class in which the field is defined.

Static import disadvantages

The overuse the static import feature can make our program unreadable and unmaintainable, polluting its namespace with all the static members we import.

Static import example

package com.zetcode; import static java.lang.Math.PI; public class JavaStaticImport < public static void main(String[] args) < System.out.println(PI); >>

In the example, we use the PI constant without its class.

Java constants

The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.

public static final double PI = 3.14159265358979323846;

For example, in java.lang.Math we have a constant named PI , whose value is an approximation of pi (the ratio of the circumference of a circle to its diameter).

Singleton pattern

Singleton design pattern ensures that one and only one object of a particular class is ever constructed during the lifetime of the application.

public class Singleton < private static final Singleton INSTANCE = new Singleton(); private Singleton() <>public static Singleton getInstance() < return INSTANCE; >>

In this simple code excerpt, we have an internal static reference to the single allowed object instance. We access the object via a static method.

In this article we have presented the Java static keyword.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

Источник

Ключевое слово this

Java-университет

Ключевое слово this <в примерах data-lazy-src=

Таким образом, здесь this позволяет не вводить новые переменные для обозначения одного и того же, что позволяет сделать код менее «перегруженным» дополнительными переменными.

Пример второй — Применение this для явного вызова конструктора

Вызов одного конструктора из другого может пригодиться тогда, когда у вас (как ни странно) несколько конструкторов и вам не хочется в новом конструкторе переписывать код инициализации, приведенный в конструкторе ранее. Запутал? Все не так страшно как кажется. Посмотрите на код ниже, в нем два конструктора класса Human :

 class Human < int age; int weight; int height; Human(int age, int weight)< this.age = age; this.weight = weight; >Human(int age, int weight, int height) < //вы вызываете конструктор с двумя параметрами this(age, weight); //и добавляете недостающую переменную this.height = height; >> 

Здесь у нас сначала приводится конструктор с двумя параметрами, который принимает int age и int weight . Допустим, мы написали в нем две строчки кода:

 this.age = age; this.weight = weight; 

а потом решили добавить еще один конструктор, с тремя параметрами, который помимо возраста и веса принимает еще и рост. В новом конструкторе вы бы могли написать так:

 this.age = age; this.weight = weight; this.height = height; 

Но вместо того, чтобы повторять уже написанный ранее код в этом конструкторе, вы можете с помощью ключевого слова this явно вызвать конструктор с двумя параметрами:

 this(age, weight); // и дописываете недостающую переменную: this.height = height; 
  • вызови this (этот) конструктор, который имеет два параметра.
  • и добавить недостающую переменную.

Ключевое слово this <в примерах data-lazy-src=

Оцените статью