static(modifier)
---------------
A modifier changes the behaviour of a member like variable/method/class/interface/enum when declared with one.
A static keyword declared with
variables.
methods.
inner classes.
blocks.
static variable
---------------
A variable declared using a keyword static is known as static variable.
static variables are automatically initialized with default values.
A static variable is a variable for which only one copy of memory is created and it is shared by all the objects of a class.
static variable is also called as shared variable.
syntax
------
accessmodifier static datatype varname=value;
ex:
public static double pi=3.14;
Note
----
when should we use instance variables and static varibles?
If the value changes from one object to another object then such of values must be declared using insatnce variables.
For example
name of student changes from one student to another student,so it is good to delcare name as instance
If a value is common for the entire class and for all objects then such type of values are declared using statuc variable.
ex:
pi=3.14
Generally we define constants as static in java.
static variables also acts as a kind of global variables.
For example
The name of college is same for all the students such type of variables must be declared as a static.
Quick questions
---------------
what is a static variable?
where should we declare static?
How many copies of memory is allocated?
static variables is also called as ---------variables.
static keyword is a --------.
static keyword is declared with -----,-----,-----,-----.
if the value of variable changes from one variable to another then declared that variable as -----
if the value is commom ,then declare as-------variable
what is an instance variable?
what is an instance method?
how many declaration scopes are there in java?
------- variable holds address of object.
variables declared in a method are called -----.
A construtor is --------method.
write four characteristics of constructor in two or 3 words.
Program
-------
class CountTest
{
int count;
public void incr()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
CountTest t1=new CountTest();
CountTest t2=new CountTest();
CountTest t3=new CountTest();
t1.incr();
t2.incr();
t3.incr();
}
}
output
------
1
1
1
Analysis
--------
In the above code we are declaring an instance variable count.
we are calling incr() method on objects t1,t2,t3,as we know every object has a seperate copy of instance variable, we get the output as
1( t1.incr())
1(t2.incr())
1(t3.incr())
as shown in the diagram.
In the above program declare variable "count" as a static variable
static int count;
then the output of the program is
1(t1.incr())
2(t2.incr())
3(t3.incr())
As shown in diagram static variable is a common variable shared by all the objects,so calling incr() on any object updates variable count.
Program
-------
class CountTest
{
static int count;
public void incr()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
CountTest t1=new CountTest();
CountTest t2=new CountTest();
CountTest t3=new CountTest();
t1.incr();//1
t2.incr();//2
t3.incr();//3
}
}
//11,11,11
//11,12,13
//10,10,10
//none
Note:
static variable save memory but declare it whenever it is necessary.
static method
-------------
A method declared using the keyword static is known as static method.
syntax
------
static accessmodifier returntype methodname()
{
//statements
}
A static method allows to access only static members(variables,methods) directly.
static members(variables,methods) are not part of object but they are part of class.
we can call static members directly using classname without creating object.
How to call static member
------------------------
static variable
---------------
syntax
------
ClassName.varname
static method
-------------
ClassName.methodname();
Program
-------
class Test
{
static int a=10;
static public void incr()
{
a++;
System.out.println(a);
}
public static void main(String args[])
{
Test.incr();
System.out.println(Test.a);
}
}
*we can access non-static members(instance variables,methods) from a static method/block by creating Object of a class.
ex:
class Test
{
int a=10;
static public void incr()
{
Test t=new Test();
t.a++;
System.out.println(t.a);
}
public static void main(String args[])
{
Test.incr();
}
}
In the above code variable "a" is an instance variable,we are accessing that variable from static method incr() by creating object for the Test class as shown in the above code.
what instance method?
Any non static method inside a class is known as instance method.
Instance varibles and instance methods are part of object,we can access them only by creating object,using either object or object reference
class Test
{
int a;
void show()
{
System.out.println("helo");
}
public static void main(String args[])
{
Test t=new Test();
t.show();
new Test().show();
}
}
Memory is allocated for instance variable,methods,blocks only when an object is created.
On the other hand static members are not part of object they are part of class so they are said to be in class scope,so we are able to call static members using classname.
Memory is allocated for static members as soon as .class file is loaded into memory.
we can exchange modifiers of a class or a method or a variable/enum/interface.
ex:
public static void m1()
{
}
static public void m1()
{
}
what is a static keyword?
what is static variable?
How many copies of memory is allocated?
static variable is also called as-----or-------.
what is a static method?
static allows to access ------ directly.
we can non static members in a static method by using ----of a clsss.
static members are part of ------.
static members are also called as-----.
instance members are part of-----.
Memory is allocated of instance members only by creating----
Memory is allocated of static members at time of ---- loading.
---- is called as instance method.
static memebers are directly accessed using ------.
Instance members must be accessed using -----or------.
Non static block inside ----and outside ---- is known as
instance block.
instance block is called ----------- whenever which is created.
instance block is used to ---------
---------------
A modifier changes the behaviour of a member like variable/method/class/interface/enum when declared with one.
A static keyword declared with
variables.
methods.
inner classes.
blocks.
static variable
---------------
A variable declared using a keyword static is known as static variable.
static variables are automatically initialized with default values.
A static variable is a variable for which only one copy of memory is created and it is shared by all the objects of a class.
static variable is also called as shared variable.
syntax
------
accessmodifier static datatype varname=value;
ex:
public static double pi=3.14;
Note
----
when should we use instance variables and static varibles?
If the value changes from one object to another object then such of values must be declared using insatnce variables.
For example
name of student changes from one student to another student,so it is good to delcare name as instance
If a value is common for the entire class and for all objects then such type of values are declared using statuc variable.
ex:
pi=3.14
Generally we define constants as static in java.
static variables also acts as a kind of global variables.
For example
The name of college is same for all the students such type of variables must be declared as a static.
Quick questions
---------------
what is a static variable?
where should we declare static?
How many copies of memory is allocated?
static variables is also called as ---------variables.
static keyword is a --------.
static keyword is declared with -----,-----,-----,-----.
if the value of variable changes from one variable to another then declared that variable as -----
if the value is commom ,then declare as-------variable
what is an instance variable?
what is an instance method?
how many declaration scopes are there in java?
------- variable holds address of object.
variables declared in a method are called -----.
A construtor is --------method.
write four characteristics of constructor in two or 3 words.
Program
-------
class CountTest
{
int count;
public void incr()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
CountTest t1=new CountTest();
CountTest t2=new CountTest();
CountTest t3=new CountTest();
t1.incr();
t2.incr();
t3.incr();
}
}
output
------
1
1
1
Analysis
--------
In the above code we are declaring an instance variable count.
we are calling incr() method on objects t1,t2,t3,as we know every object has a seperate copy of instance variable, we get the output as
1( t1.incr())
1(t2.incr())
1(t3.incr())
as shown in the diagram.
In the above program declare variable "count" as a static variable
static int count;
then the output of the program is
1(t1.incr())
2(t2.incr())
3(t3.incr())
As shown in diagram static variable is a common variable shared by all the objects,so calling incr() on any object updates variable count.
Program
-------
class CountTest
{
static int count;
public void incr()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
CountTest t1=new CountTest();
CountTest t2=new CountTest();
CountTest t3=new CountTest();
t1.incr();//1
t2.incr();//2
t3.incr();//3
}
}
//11,11,11
//11,12,13
//10,10,10
//none
Note:
static variable save memory but declare it whenever it is necessary.
static method
-------------
A method declared using the keyword static is known as static method.
syntax
------
static accessmodifier returntype methodname()
{
//statements
}
A static method allows to access only static members(variables,methods) directly.
static members(variables,methods) are not part of object but they are part of class.
we can call static members directly using classname without creating object.
How to call static member
------------------------
static variable
---------------
syntax
------
ClassName.varname
static method
-------------
ClassName.methodname();
Program
-------
class Test
{
static int a=10;
static public void incr()
{
a++;
System.out.println(a);
}
public static void main(String args[])
{
Test.incr();
System.out.println(Test.a);
}
}
*we can access non-static members(instance variables,methods) from a static method/block by creating Object of a class.
ex:
class Test
{
int a=10;
static public void incr()
{
Test t=new Test();
t.a++;
System.out.println(t.a);
}
public static void main(String args[])
{
Test.incr();
}
}
In the above code variable "a" is an instance variable,we are accessing that variable from static method incr() by creating object for the Test class as shown in the above code.
what instance method?
Any non static method inside a class is known as instance method.
Instance varibles and instance methods are part of object,we can access them only by creating object,using either object or object reference
class Test
{
int a;
void show()
{
System.out.println("helo");
}
public static void main(String args[])
{
Test t=new Test();
t.show();
new Test().show();
}
}
Memory is allocated for instance variable,methods,blocks only when an object is created.
On the other hand static members are not part of object they are part of class so they are said to be in class scope,so we are able to call static members using classname.
Memory is allocated for static members as soon as .class file is loaded into memory.
we can exchange modifiers of a class or a method or a variable/enum/interface.
ex:
public static void m1()
{
}
static public void m1()
{
}
what is a static keyword?
what is static variable?
How many copies of memory is allocated?
static variable is also called as-----or-------.
what is a static method?
static allows to access ------ directly.
we can non static members in a static method by using ----of a clsss.
static members are part of ------.
static members are also called as-----.
instance members are part of-----.
Memory is allocated of instance members only by creating----
Memory is allocated of static members at time of ---- loading.
---- is called as instance method.
static memebers are directly accessed using ------.
Instance members must be accessed using -----or------.
Non static block inside ----and outside ---- is known as
instance block.
instance block is called ----------- whenever which is created.
instance block is used to ---------
No comments:
Post a Comment