[go: up one dir, main page]

0% found this document useful (0 votes)
19 views20 pages

UNIT I (Static Keyword)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views20 pages

UNIT I (Static Keyword)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIT-I

(PART-II)
JAVA ANATOMY
TOPICS:
 JAVA Objects and References
 Constructors
 this Keyword
 Arrays
 Strings and Immutability
 Buffer and Builder Classes
 String Tokenizer
STATIC KEYWORD
Definition : The static keyword in Java
is used for memory management
mainly.
We can apply java static keyword with
variables, methods, blocks and nested
class.
The static keyword belongs to the
class than an instance of the class.
The static can be:
1. Variable (also known as a class variable)
2. Method (also known as a class method)
3. Block
4. Nested class
1.Java Static Variable: If you declare any
variable as static, it is known as a
static variable.
The static variable can be used to refer
to the common property of all objects
(which is not unique for each object).
 for example, the company name of
employees, college name of students,
etc.
The static variable gets memory only
once in the class area at the time of
class loading.
Advantages :
1. It saves Memory.
2. Java Static Property is shared to all
Objects

Syntax for declare static variable:


static variableName;
problem without static variable :
class Student
{  
    int rollno;  
String name;  
String college="ITS";  
}  
Suppose there are 500 students in my college, now all
instance data members will get memory each time
when the object is created. All students have its
unique rollno and name, so instance data member is
good in such case. Here, "college" refers to the
common property of all objects. If we make it static,
this field will get the memory only once.
Eg-1: /* Static Variable*/
O/P : 111 Tom ITS
Contd… 222 Jerry ITS
Static Memory Allocation
Eg-2: /* Program with out using Static Variable */
• In this example, we have created an
instance variable named count which is
incremented in the constructor.
• Since instance variable gets the memory at
the time of object creation, each object will
have the copy of the instance variable.
• If it is incremented, it won't reflect other
objects. So each object will have the value 1
in the count variable.
Eg-3: /* Program with using Static Variable */
2.Java Static Method: If you apply static
keyword with any method, it is known
as static method.
 A static method belongs to the class
rather than the object of a class.
 A static method can be invoked
without the need for creating an
instance of a class.
 A static method can access static
data member and can change the
value of it.
Eg-1: /* Program with & with out using static method */
O/P : Hello I am Non-Static
Hello I am Static
Eg-2: /* Program with using static method */
O/P : 125
Q) Why is the Java main method static?
Ans) It is because the object is not required
to call a static method.
 If it were a non-static method, JVM creates
an object first then call main() method
 So that will lead the problem of extra
memory allocation.
3.Java Static Block: It Is used to initialize
the static data member.
 It is executed before the main method at the
time of class loading.
O/P : Static Block is Invoked
Hello main
Q)  Can we execute a program without
main() method?
Ans) No, one of the ways was the static
block, but it was possible till JDK 1.6.
Since JDK 1.7, it is not possible to
execute a java class without the main
method.
Eg-1: /* Program with out using main method
*/

Output: static block is invoked


Since JDK 1.7 and above, output would be:
Error: Main method not found in class A3, please define
the main method as: public static void main(String[]
args)

You might also like