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;
- package com.instanceofjavastatic;
- class StaticDemo{
- static int a=40;
- static int b=60;
- }
- 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.
- package com.instanceofjava;
- class StaticDemo{
- static int a=10;
- static int b=20;
- public static void main(String [] args){
- //local variables should not be static
- static int a=10;// compile time error: illegal start of expression
- }
- }
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.
- static void show(){
- }
- 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.
Read more @ what is static method in java
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.
- static{
- }
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.
Read more @ Can we overload static methods in java
9.Can we override static methods in java?
- NO we can not override static methods in java.
Read more @ Can we 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.
- package instanceofjava;
- public MainDemo{
- static public void main(String [] args){
- }
- }
- Yes we can call super class static methods from sub class.
- Read more @ Can we call super class static methods from sub class?
13.How to call non static method from static method java
Java programming interview questions and answers
- Print prime numbers?
- What happens if we place return statement in try catch blocks
- Write a java program to convert binary to decimal
- Java Program to convert Decimal to Binary
- Is it possible to print message without using System.out.println()
- Java program to restrict a class from creating not more than three objects
- Java basic interview programs on this keyword
- Java Program to Sort elements of Java ArrayList Example
- Interfaces allows constructors?
- Can we create static constructor in java
- Super keyword interview questions java
- Java interview questions on final keyword
- Can we create private constructor in java
- Java Program Find Second highest number in an integer array
- Java interview programming questions on interfaces
- Top 15 abstract class interview questions
- Java interview Questions on main() method
- Sort employee object by id in descending order using comparable and TreesSet
- Top 20 collection framework interview Questions
- Java Interview Program to find smallest and second smallest number in an array
- Java Coding Interview programming Questions : Java Test on HashMap
- Explain java data types with example programs
- How to check internet connection using java
- Constructor chaining in java with example programs
- Top 10 Interview Programs and questions on method overriding in java
- Swap two numbers without using third variable in java
- Find sum of digits in java
- How to create immutable class in java
- AtomicInteger in java
- Check Even or Odd without using modulus and division
- String Reverse Without using String API
- Find Biggest substring in between specified character
- Check string is palindrome or not?
- Reverse a number in java?
- Fibonacci series with Recursive?
- Fibonacci series without using Recursive?
- Sort the String using string API?
- Sort the String without using String API?
- what is the difference between method overloading and method overriding?
- How to find largest element in an array with index and value ?
- Sort integer array using bubble sort in java?
- Object Cloning in java example?
- Method Overriding in java?
- Program for create Singleton class?
- Print numbers in pyramid shape?
- Check armstrong number or not?
- Producer Consumer Problem?
- Remove duplicate elements from an array
- Convert Byte Array to String
- Print 1 to 10 without using loops
- Add 2 Matrices
- Multiply 2 Matrices
- How to Add elements to hash map and Display
- Sort ArrayList in descending order
- Sort Object Using Comparator
- Count Number of Occurrences of character in a String
- Can we Overload static methods in java
- Can we Override static methods in java
- Can we call super class static methods from sub class
- Explain return type in java
- Can we call Sub class methods using super class object?
- Can we Override private methods ?
- Basic Programming Questions to Practice : Test your Skill
- Java programming interview questions on collections
No comments