The Artima Developer Community
Sponsored Link

Java Buzz Forum
Top 10 Java Interview Questions on Static keyword

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Top 10 Java Interview Questions on Static keyword Posted: Jul 24, 2015 8:37 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Top 10 Java Interview Questions on Static keyword
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

1.what is static in java?

  • Static is a keyword in java.
  • One of the Important keyword in java.
  • Clear understanding of static keyword is required to build projects.
  • We have static variables, static methods , static blocks. 
  • Static means class level.

2.Why we use static keyword in java?

  • Static keyword is mainly used for memory management.
  • Static variables get memory when class loading itself.
  • Static variables can be used to point common property all objects.

3.What is static variable in java?

  • Variables declared with static keyword is known as static variables.
  • Static variables gets memory on class loading.
  • Static variables are class level.
  • If we change any static variable value using a particular object then its value changed for all objects means it is common to every object of that class.
  • static int a,b;


  1. package com.instanceofjavastatic;
  2. class StaticDemo{
  3.  
  4. static int a=40;
  5. static int b=60;
  6.  
  7. }

  • We can not declare local variables as static it leads to compile time error "illegal start of expression".
  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.

  1. package com.instanceofjava;
  2. class StaticDemo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  public static void main(String [] args){
  7.  
  8.    //local variables should not be static
  9.  static int a=10;// compile time error: illegal start of expression
  10. }
  11. }

 Read more : Explain about static variables in java?

4. what is static method in java with example

  • Method which is having static in its method definition is known as static method.
  1. static void show(){
  2.  
  3. }
  • JVM will not call these static methods automatically. Develioper needs to call these static methods from main method or static block or variable initialization.
  • Only Main method will b called by JVM automatically.
  • We can call these static methods by using class name itself no need to create object.

 5.what is static block in java?

  •  Static blocks are the blocks which will have braces and with static keyword.
  • These static blocks will be called when JVM loads the class.
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.
  •  
    1. static{ 
    2.  
    3.  }

 Read more @ Static blocks in java with example programs

 6.What is the need of static block?

  • Static blocks will be executed at the time of class loading.
  • So if you want any logic that needs to be executed at the time of class loading that logic need to place inside the static block so that it will be executed at the time of class loading.

7.Why main method is static in java?

  • To execute main method without creating object then the main method should be static so that JVM will call main method by using class name itself.

8.Can we overload static methods in java?

  • Yes we can overload static methods in java.

9.Can we override static methods in java?

  • NO we can not override static methods in java.

10.Can we write static public void main(String [] args)?


  • Yes we can define main method like static public void main(String[] args){}
  •  Order of modifiers we can change.

  1. package instanceofjava;
  2. public MainDemo{
  3. static public void main(String [] args){
  4.  
  5. }
  6. }


Read: Top 10 Java Interview Questions on Static keyword

Topic: DevOps Mumbo Jumbo & Zombies - thwackCamp panel Previous Topic   Next Topic Topic: Matching journalism to the medium

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use