Saturday, December 8, 2018

static keyword


The static keyword in Java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than an instance of the class.
The static can be:
  1. Variable (also known as a class variable)
  2. Method (also known as a class method)
  3. Block
  4. Nested class
1) Java static variable
If you declare any variable as static, it is known as a static variable.
  • The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc.
  • The static variable gets memory only once in the class area at the time of class loading.
Advantages of static variable
It makes your program memory efficient (i.e., it saves memory).
2) Java static method
If you apply static keyword with any method, it is known as static method.
  • A static method belongs to the class rather than the object of a class.
  • A static method can be invoked without the need for creating an instance of a class.
  • A static method can access static data member and can change the value of it.
Restrictions for the static method
There are two main restrictions for the static method. They are:
  1. The static method can not use non static data member or call non-static method directly.
  2. this and super cannot be used in static context.
Q) Why is the Java main method static?
Ans) It is because the object is not required to call a static method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.

3) Java static block
  • Is used to initialize the static data member.
  • It is executed before the main method at the time of classloading.

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

Ans) No, one of the ways was the static block, but it was possible till JDK 1.6. Since JDK 1.7, it is not possible to execute a java class without the main method.


10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Surely, this post will help you find the best IDEs for your project. Still, you find any confusion and don’t want to take a risk. You can Hire PHP Developer from the best PHP web development company, who is skilled and has worked on the latest trends. He will not only help you select the best IDE tools considering your project requirements but also will give the physical form to your project.

    ReplyDelete
  3. Local Cash for Car is a licensed auto removal company that buys all types of used, junk, scrap, broken, and damaged vehicles for as much as up to Cash For Cars Kelvin Grove

    ReplyDelete
  4. Thanks for sharing such a great post I like your way of writing. We purchase any type of old, damaged, scrap, junk, wrecked, and unwanted vehicles for as much as up to $10000 One of the prime benefits of choosing us is that you will also get free car removal anywhere Cash For Scrap Cars

    ReplyDelete
  5. We offer the best value for money. We provide same-day car removal throughout Brisbane and surrounding suburbs. Brisbane Car Removalists can offer the highest possible cash settlement for your vehicle no matter the make, model, or condition of the car. We will take Utes, vans, trucks, undrivable and unregistered cars and will pay up to $5,999. As a result, this makes us one of the most competitive car buyers in Brisbanecar removal

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. If you are considering getting cash for unwanted cars gold coast, you may have heard of a place called cash for junk cars Gold Coast. We will provide you with some tips on how you can sell your car to someone for cash. Hopefully, this article will be a helpful resource to you as you look for ways to sell your car for cash. Remember that you don't have to spend hours looking for a place to sell your junk car.

    ReplyDelete