0% found this document useful (0 votes)
156 views

Static Keyword in Java: Static Is A Non-Access Modifier in Java Which Is Applicable For

push() method syntax: stack_name.push(data); Where data is the element that needs to be pushed into the stack. For example: Stack<Integer> stack = new Stack<Integer>(); stack.push(10); This will push the element 10 into the stack.

Uploaded by

project mission
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Static Keyword in Java: Static Is A Non-Access Modifier in Java Which Is Applicable For

push() method syntax: stack_name.push(data); Where data is the element that needs to be pushed into the stack. For example: Stack<Integer> stack = new Stack<Integer>(); stack.push(10); This will push the element 10 into the stack.

Uploaded by

project mission
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

static keyword in java

static is a non-access modifier in Java which is applicable for


the following:

1. blocks
2. variables
3. methods
4. nested classes

When a member is declared static, it can be accessed before any


objects of its class are created, and without reference to any
object.

Static variables

When a variable is declared as static, then a single copy of


variable is created and shared among all objects at class level.
Static variables are, essentially, global variables. All instances
of the class share the same static variable

Important points for static variables :-

 We can create static variables at class-level only. See here


 static block and static variables are executed in order they
are present in a program.
// java program to demonstrate execution
// of static blocks and variables
class Test
{
// static variable
static int a = m1();
// static block
static {
System.out.println("Inside static block");
}
// static method
static int m1() {
System.out.println("from m1");
return 20;
}
// static method(main !!)
public static void main(String[] args)
{
System.out.println("Value of a : "+a);
System.out.println("from main");
}

Output:

from m1
Inside static block
Value of a : 20
from main
Super Keyword in Java
The super keyword in java is a reference variable that is used to
refer parent class objects.  The keyword “super” came into the
picture with the concept of Inheritance. It is majorly used in the
following contexts:

1. Use of super with variables: This scenario occurs when a


derived class and base class has same data members. In that case
there is a possibility of ambiguity for the JVM. We can
understand it more clearly using this code snippet:

/* Base class vehicle */


class Vehicle
{
    int maxSpeed = 120;
}
  
/* sub class Car extending vehicle */
class Car extends Vehicle
{
    int maxSpeed = 180;
  
    void display()
    {
        /* print maxSpeed of base class (vehicle) */
        System.out.println("Maximum Speed: " +
super.maxSpeed);
    }
}
  
/* Driver program to test */
class Test
{
    public static void main(String[] args)
    {
        Car small = new Car();
        small.display();
    }
}

Output:

Maximum Speed: 120

In the above example, both base class and subclass have a


member maxSpeed. We could access maxSpeed of base class in
sublcass using super keyword.

Private keyword

 The private access modifier is accessible only within the


same class.
 We can't assign private to outer class and interface.
 The best use of private keyword is to create a fully
encapsulated class in Java by making all the data
members of that class private.
 If we make any class constructor private, we cannot
create the instance of that class from outside the class.
 If we are overriding any method, overridden method (i.e.,
declared in the subclass) must not be more restrictive.
 According to the previous point, if we assign a private
modifier to any method or variable, that method or
variable can be overridden to sub-class using all type of
access modifiers. However, still, we can't invoke private
method outside the class.
Encapsulation
.Encapsulation can be achieved by: Declaring all the
variables in the class as private and writing public methods
in the class to set and get the values of variables.

Interfaces

Why do we use interface ?


 It is used to achieve total abstraction.
 Since java does not support multiple inheritance in case of class, but by using interface it
can achieve multiple inheritance .
 It is also used to achieve loose coupling.
 Interfaces are used to implement abstraction. So the question arises why use interfaces
when we have abstract classes?
The reason is, abstract classes may contain non-final variables, whereas variables in interface are
final, public and static.
Inner classes
we can’t have static method in a nested inner class because an inner class is implicitly associated
with an object of its outer class so it cannot define any static method for itself. 

Stack class in java

Syntax for push() method.

Stack <name of data type> stack_name=new Stack<name of


data type>();

You might also like