0% found this document useful (0 votes)
52 views2 pages

Core Java Static

The static keyword in Java allows variables, methods, and nested classes to be declared as static. Static variables and methods belong to the class itself rather than any object, so they can be accessed before any objects are created and without creating an instance. A static variable only has one copy shared among all instances of the class, while a non-static variable has a unique copy for each instance. Static methods can only access static data and call other static methods, while non-static methods can access both static and non-static data and call both static and non-static methods. The final keyword prevents a variable from being modified after initialization or a method from being overridden in a subclass.

Uploaded by

Karthika N2S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
52 views2 pages

Core Java Static

The static keyword in Java allows variables, methods, and nested classes to be declared as static. Static variables and methods belong to the class itself rather than any object, so they can be accessed before any objects are created and without creating an instance. A static variable only has one copy shared among all instances of the class, while a non-static variable has a unique copy for each instance. Static methods can only access static data and call other static methods, while non-static methods can access both static and non-static data and call both static and non-static methods. The final keyword prevents a variable from being modified after initialization or a method from being overridden in a subclass.

Uploaded by

Karthika N2S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

Definition of Static

Static is a keyword, applicable to the classes, variables, methods, and blocks. The class members,
class, and blocks can be made static using “static” keyword in front of the name of the class
members, class, and blocks respectively. When a class member is declared as static, it becomes
global for all other members of the class. The static member of the class does not occupy
memory on per instance basis, i.e. all the objects shares the same copy of static member. The
static member can be used independently of any object of that class. You can access the static
member of the class before its object is created. The best example of the static member is main( )
method, it is declared static so that it can be invoked before any object exists. The general form
to access the static member of the class:

1. class_name.static_member // accessing static member of the class

In above code class_name is the name of the class in which the static_member is defined. Static
member can be a static variable or static method

Static variables:

 A static variable acts like a global variable for all other data members of the class.
 A static variable can be accessed before any object of the class exists.
 A static variable can be accessed with the class name in which it is defined followed by the
dot(.) operator.

Static Methods:

 A static method can only call other static methods only.


 A static method can access static data only.
 A static method can not be referred to “this” or “super” in any conditions.
 A static method can be accessed with the class name in which it is defined followed by the
dot(.) operator.

Static class:

 Java does have the concept of nested static class. The outermost class can not be made
static whereas the innermost class can be made static.
 A static nested class can not access the non-static member of the outer class.
 It can only access the static members of the outer class.

Static Block:

Static block is executed only once when the class is loaded. Used to initialize the static variables
of the class.
C++:

In C++ we have the concept of static variables as well as static functions whereas, C++ do not
support static class.

C#:

C# supports static class, static variables, and static Class also.

Java:

Java supports static nested class, static variables, static methods.

Definition of Final

Final is a keyword applicable to the class, variable and methods. The class, variable and the
method is declared as final using the keyword “final” preceded by their name. Once a variable is
declared as final; it can not be modified further in the program. A final variable must be
initialized at the time declaration. Final variables do not occupy memory on per-instance basis.
All the objects of the classes share the same copy of the final variable.

The method declared as final can not be overridden by the subclass of that class in which final
method is declared. When a class is declared as final other class can not inherit that final class.
C++, C# do not support the concept if final keyword. Java supports the concept of final keyword
and in Java; class, variable, and method can be declared as final.

You might also like