0% found this document useful (0 votes)
8 views6 pages

Core Java - OOPs Concepts Static Keyword

The document discusses the static keyword in Java, explaining static variables, methods, and blocks, as well as their characteristics and restrictions. It highlights the importance of the static main() method as the entry point for Java programs and clarifies that static methods cannot be overridden or used with non-static members. Additionally, it addresses the use of static variables and methods in abstract classes, along with examples for better understanding.

Uploaded by

amitvani2024
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)
8 views6 pages

Core Java - OOPs Concepts Static Keyword

The document discusses the static keyword in Java, explaining static variables, methods, and blocks, as well as their characteristics and restrictions. It highlights the importance of the static main() method as the entry point for Java programs and clarifies that static methods cannot be overridden or used with non-static members. Additionally, it addresses the use of static variables and methods in abstract classes, along with examples for better understanding.

Uploaded by

amitvani2024
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/ 6

Core Java - OOPs Concepts: static Keyword

Interview Questions

39) What is the static variable?


The static variable is used to refer to the common property of all objects (that
is not unique for each object), for example, the company name of employees,
the college name of students, etc. Static variable gets memory only once in
the class area at the time of class loading. Using a static variable makes your
program more memory efficient (it saves memory). A static variable belongs
to the class rather than the object. The following Java program depicts the
use of a static variable.

Example
​ public class Main {
​ int rollno;
​ String name;
​ static final String COLLEGE = "ITS";
​ Main(int r, String n) {
​ rollno = r;
​ name = n;
​ }
​ void display() {
​ System.out.println(rollno + " " + name + " " + COLLEGE);
​ }
​ public static void main(String[] args) {
​ Main obj1 = new Main (111, "Karan");
​ Main obj2 = new Main(222, "Aryan");
​ obj1.display();
​ obj2.display();
​ }
​ }
Compile and Run
Output:

111 Karan ITS


222 Aryan ITS
40) What is the static method?
In Java, a static variable is a class-level variable that belongs to the class
rather than to instances of the class. It is shared among all instances of the
class and is initialized only once when the class is loaded into memory.

○​ A static method belongs to the class rather than the object.


○​ There is no need to create the object to call the static methods.
○​ A static method can access and change the value of the static variable.

Static Keyword in Java

41) What are the restrictions that are applied to the Java
static methods?
Java static methods have certain restrictions and characteristics that
differentiate them from instance methods. Here are the key restrictions and
considerations for Java static methods:

The two main restrictions applied to the static methods are as follows:

○​ The static method cannot use a non-static data member or call a non-static
method directly.
○​ This and super keyword cannot be used in a static context as they are
non-static.
○​ Static methods in Java cannot be overridden. Even if a subclass declares a
static method with the same signature as a static method in its superclass, it
is considered method hiding, not overriding.

To read more: Static Method in Java

42) Why the main() method is static?


Because the object is not required to call the static method, if we make the
main() method non-static, the JVM will have to create its object first and then
call the main() method, which will lead to extra memory allocation.

Here are the main reasons:


The main() method is the entry point for the execution of a Java program.
When we run a Java program, the Java Virtual Machine (JVM) looks for the
main() method with the following signature:

​ public static void main(String[] args)


When a Java program is executed, the JVM loads the class into memory and
looks for the main() method to start the program.

To read more: Why main() method is always static in Java

43) Can we override the static methods?


No, static methods cannot be overridden in Java. While a subclass can
declare a static method with the same signature as a static method in its
superclass, it is considered method hiding, not method overriding.

44) What is the static block?


A static block is used to initialize the static data member. It is executed before
the execution of the main() method, at the time of class loading.

Example
​ class Main {
​ static { System.out.println("static block is invoked"); }
​ public static void main(String args[]) {
​ System.out.println("Hello main");
​ }
​ }
Compile and Run
Output:

static block is invoked


Hello main

To read more: Static Block in Java

45) Can we execute a program without a main() method?


No, in Java, we cannot execute a program without the main() method. The
main() method serves as the entry point for the execution of a Java program,
and it must be defined in every Java class that serves as the starting point for
an application. The JVM looks for the main() method with the following
signature to begin program execution:

​ public static void main(String[] args)


To read more: Java main() Method

46) What if the static modifier is removed from the


signature of the main method?
Program compiles. If the static keyword is removed from the signature of the
main() method in Java, the program will compile successfully, but it will not
run successfully. The main() method must be declared as static because it
serves as the entry point for the Java program, and the Java Virtual Machine
(JVM) needs to invoke it without creating an instance of the class. It throws
an error "NoSuchMethodError."

47) What is the difference between a static (class) method


and an instance method?

A method that is declared as static is A method that is not declared as static


known as a static method. is known as an instance method.

We do not need to create the objects to The object is required to call the
call the static methods. instance methods.

Non-static (instance) members cannot Static and non-static variables can


be accessed in the static context both be accessed in instance methods.
(static method, static block, and static
nested class) directly.
For example: public static int cube(int For example: public void msg(){...}.
n){ return n*n*n;}

To read more: Difference Between Static and Instance Methods in Java

48) Can we make constructors static?


No, constructors cannot be declared as static in Java, as we know that the
static context (method, block, or variable) belongs to the class, not the object.
Since Constructors are invoked only when the object is created, there is no
sense in making the constructors static. Making a constructor static would
imply that it belongs to the class and not to instances, which contradicts the
fundamental purpose of a constructor. However, if we try to do so, the
compiler will show the compiler error..

49) Can we make the abstract methods static in Java?


No, abstract methods cannot be declared as static in Java. In Java, if we make
the abstract methods static, they will become part of the class, and we can
directly call them, which is unnecessary. Since abstract methods are expected
to be overridden by subclasses, making them static would be contradictory to
their purpose. Calling an undefined method is completely useless; therefore, it
is not allowed.

50) Can we declare the static variables and methods in an


abstract class?
Yes, it is possible to declare static variables and methods in an abstract class
in Java. As we know that there is no requirement to make the object access
the static context; therefore, we can access the static context declared inside
the abstract class by using the name of the abstract class.

Consider the following example.

Example
​ abstract class Test
​ {
​ static int i = 102;
​ static void display()
​ {
​ System.out.println("hi !! I am good !!");
​ }
​ }
​ public class Main extends Test
​ {
​ public static void main (String args[])
​ {
​ Test.display();
​ System.out.println("i = "+Test.i);
​ }
​ }
Compile and Run
Output

hi !! I am good !!
i = 102

nterview

You might also like