Static Variable And Static Method Flashcards

1
Q

What are Static variable?

A

Static variable in Javais variable which belongs to the class and initialized only once at the start of the execution.
It is a variable which belongs to the class and not to object(instance ).
Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.

single copy to be shared by all instances of the class

A static variable can be accessed directly by the class name and doesn’t need any object

Syntax:

<class-name>.<variable-name>
</variable-name></class-name>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Static method?

A

Static method in Javais a method which belongs to the class and not to the object. A static method can access only static data. It is a method which belongs to the class and not to the object(instance). A static method can access only static data. It cannot access non-static data (instance variables).

A static method can call only other static methods and can not call a non-static method from it.

A static method can be accessed directly by the class name and doesn’t need any object

A static method cannot refer to “this” or “super” keywords in anyway

Syntax:

<class-name>.<method-name>

Note:main method is static, since it must be accessible for an application to run, before any instantiation takes place.
</method-name></class-name>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Static Block in Java?

A

Thestatic blockis a block of statement inside a Java class that will be executed when a class is first loaded into theJVM. Astatic block helps to initialize the static data members, just like constructors help to initialize instance members.

class Test{ static { //Code goes here } }

Following program is the example of java static block.

Example: How to access static block

1

public class Demo {

2

static int a;

3

static int b;

4

static {

5

a = 10;

6

b = 20;

7

}

8

public static void main(String args[]) {

9

10

System.out.println(“Value of a = “ + a);

11

System.out.println(“Value of b = “ + b);

12

13

}

14

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Static method and static variable

A

Donate

Learn to code —free 3,000-hour curriculum

MARCH 7, 2023/#JAVA

Static Variables in Java – Why and How to Use Static Methods

Edeh Israel Chidera

Static variables and static methods are two important concepts in Java.

Whenever a variable is declared as static, this means there is only one copy of it for the entire class, rather than each instance having its own copy. A static method means it can be called without creating an instance of the class.

Static variables and methods in Java provide several advantages, including memory efficiency, global access, object independence, performance, and code organization.

In this article, you will learn how static variables work in Java, as well as why and how to use static methods.

The Static Keyword in Java

The static keyword is one of the most essential features in the Java programming language. We use it to define class-level variables and methods.

Here is an example of how to use the static keyword:

public class StaticKeywordExample { private static int count = 0; // static variable public static void printCount() { // static method System.out.println(“Number of Example objects created so far: “ + count); } }

As you can see above, we declared thecountvariable as a static variable, while we declared theprintCountmethod as a static method.

When a variable is declared static in Java programming, it means that the variable belongs to the class itself rather than to any specific instance of the class. This means that there is only one copy of the variable in memory, regardless of how many instances of the class are created.

Here’s an example. Say we have aDepartmentclass that has a static variable callednumberOfWorker. We declare and increment the static variable at the constructor level to show the value of the static variable whenever the class object is created.

public class Department{ public static int numberOfWorker= 0; public String name; public Department(String name) { this.name = name; numberOfWorker++; // increment the static variable every time a new //Person is created } }

The results of the above code show that as we create new Department objects, the static variablenumberOfWorkerretains its value.

When we print out the value ofnumberOfWorkerin the console, we can see that it retains its value across all instances of theDepartmentclass. This is because there is only one copy of the variable in memory, and any changes to the variable will be reflected across all instances of the class.

Department dpt1 = new Department(“Admin”); System.out.println(Department.numberOfWorker); // output: 1 Department dpt2 = new Department (“Finance”); System.out.println(Department.numberOfWorker); // output: 2 Department dpt3 = new Department (“Software”); System.out.println(Department.numberOfWorker); // output: 3

We can also use thestatickeyword to define static methods.

Static methods are methods that belong to the class rather than to any specific instance of the class. Static methods can be called directly on the class itself without needing to create an instance of the class first. See the code below:

public class Calculation{ public static int add(int a, int b) { return a + b; } public static int multiply(int a, int b) { return a * b; } }

In the above code, theCalculationclass has two static methods. The declared static methods can be called directly on the Calculation class without creating an instance of the class first. That is to say, you do not need to create an object of the Calculation class before you access the staticaddandmultiplyclasses.

int result = Calculation.add(5, 10); System.out.println(result); // Output: 15 int result2 = Calculation.multiply(5, 10); System.out.println(result2); // Output: 50

Themain()method in Java is an example of a static method. Themain()method is a special static method that is the entry point for Java applications. TheMathclass in Java also provides many static methods that perform mathematical operations.

public class Main { public static void main(String[] args) { System.out.println(“Hello, World!”); } } int result = Math.max(5, 10); System.out.println(result); // Output: 10

The above code shows that the entry point for Java applications is a static method. It also shows that themax()method is a static method of the Math class and does not require an instance of the Math class to be created.

As you can see, static methods can be useful in providing utility functions that do not necessitate the creation of a class object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Static variable and method summary

A

Staticvariables and methods belong to a class and are called with the Class Name rather than using object variables, like ClassName.methodName();

There is only one copy of a static variable or method for the whole class. For example, the main method is static because there should only be 1 main method.

Static methods can be public or private.

The static keyword is placed right after the public/private modifier and right before the type of variables and methods in their declarations.

class ClassName { // static variable public static type variableName; // static method public static returnType methodName(parameters) { // implementation not shown } } // To call a static method or variable, use the Class Name System.out.println(ClassName.staticVariable); ClassName.staticMethod();

Static methods only have access to other static variables and static methods. Static methods cannot access or change the values of instance variables or the this reference (since there is no calling object for them), and static methods cannot call non-static methods. However, non-static methods have access to all variables (instance or static) and methods (static or non-static) in the class.

Since there is only 1 copy of astaticvariable or method, static variables are often used to count how many objects are generated. In the following classPerson, there is astaticvariable calledpersonCounterthat is incremented each time thePersonconstructor is called to initialize a newPersonobject. The static methodprintCounterprints out its value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly