+ 1
What is static keyword? And why it is used? Need a clear explaination with some examples
need more information on static keyword like what does it do,why it is used,what Is it's benefit,how memory allocation is done
4 Respostas
+ 3
If we define a member of a class as static, it can be invoked without creating an object first.
Example,
public class something{
public static void main(String[] args){
Dog stuart=new Dog();//creating
// object
int b=stuart.bark();
}
public int bark(int m){
do something;
}
}
-----------------------------------------------------
if static is used
public class something{
public static void main(String[] args){
int b= A.bark();
//As you can see , I didnt creat an object
//first
//This is because the method "bark" is
//static
}
public static int bark(int m){
Do something;
}
}
+ 1
READ 1st
The keyword static can be used in 4 ways. With a a code block, class variable, a class method, or an inner/nested class.
Static code block:
==============
A static block is used to initialize a static data member and is executed prior to the main method when the class is loading.
class Program {
static{ System.out.println("I run before the main method is invoked"); }
public static void main(String args[]) {
System.out.println("I run after the static block");
}
}
Static Variable:
============
When static is used with a class variable that variable belongs to all instances created from that class and its value is shared amongst them. If any instance sets or changes a static variable its value is changed across all instances of that class. A static variable is used for a property that is common to all objects of that class. Its memory is only created once no matter how may objects of that class are instantiated.
Example: Let's say we have a class called ThrowingStar with a static int variable named count that is incremented whenever a new ThrowingStar is instantiated or picked up and decremented whenever a ThrowingStar is thrown or destroyed.
ThrowingStar a = new ThrowingStar();
ThrowingStar b = new ThrowingStar();
// Here we have 2 new ThrowingStar instances "a" and "b".
// When they are created the static int count variable is incremented to 2.
// We use the getter method to retrieve the value of count for "a" and then "b".
System.out.println(a.getCount()); // outputs 2
System.out.println(b.getCount()); // outputs 2
// Now we use the throw method of "a" to throw it at an enemy which decrements the count by 1.
a.throw(); // This will reduce the value of the static variable count to 1 for both "a" and "b"
// again we retrieve the value of count for both "a" and "b"
System.out.println(a.getCount()); // now outputs 1
System.out.println(b.getCount()); // now outputs 1
+ 1
READ 2nd
Static class method:
================
A static method belongs to the class itself rather than belonging to an instance of the class. A static method can be used without the need to create or use an instance of the class in order to call that method. You have probably already used these types of methods without even knowing it. For instance the println() method of System.out is a static method. Also, this is why main (the entry to our Java programs) is static. It wouldn't make much sense if we needed to create an instance of our class that held the main method prior to running our program.
Note that a static method can not use non-static data members/variables or methods directly. They also can't use the keywords this or super within their context.
Example: Let's make the getCount method from the ThrowingStar class static.
class ThrowingStar {
static int count;
// some code
static int getCount() {
if( count != null) {
return count;
}
return 0;
}
// some other code
}
Now instead of using a.getCount() or b.getCount() we can just use the class itself:
System.out.println(ThrowingStar.getCount()); // outputs the current count of throwing stars.
Static nested class:
===============
The only time a class may be declared static is when that class is a nested or inner class. A static class can not access non-static members of the outer class and can only access its static members. A public instance of a nested static can be created without creating an instance of its outer class while a non-static nested class would require an instance of its outer class to be created first.
class Outer {
public static class StaticInner{}
public class Inner{}
}
// create an instance of the inner static class
Outer.StaticInner staticClass = new Outer.StaticInner();
// create an instance of the non-static inner class
Outer outerClass = new Outer();
outerClass.Inner innerClass = outerClass.new Inner();
// OR
Outer.Inner innerClass2 = new Outer().new Inner();
0
a class that returns nothing