The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Static Constructor : is it possible to create static constrcutor?

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.
Java Static Constructor : is it possible to create static constrcutor? Posted: Feb 25, 2016 12:21 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java Static Constructor : is it possible to create static constrcutor?
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.Is it possible to create static constructor in java?
  • No. We can not create constructor with static.
  • If we try to create a static constructor compile time error will come: Illegal modifier for the constructor
Static construcotr in java interview question


2.What is the real use of constructor in java? 

  • Constructors will be used to assign instance variables with default values.
  • Whenever object is created constructor will be called so the default values for the instance variables will be assigned in this constructor.
  • Top 15 Java Interview Questions on Constructors

  1. public class ConstructorDemo {
  2.  
  3.  int a,b;

  4. ConstructorDemo (int x, int y){
  5.  
  6. a=x;
  7. b=y;
  8.  
  9. }
  10. public static void main(String[] args) {
  11.  
  12.         ConstructorDemo ob= new ConstructorDemo ();
  13.  
  14.     }
  15. }


3. What is the use of static 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.
4.Is there any alternative solution for static constructor in java

  • Static means class level.
  • Constructor will be use to assign initial values for instance variables
  • static and constructor are different and opposite from each other.
  • To assign initial values for instance variable we use constructor.
  • To assign static variables we use Static Blocks
  • We can use static blocks to initialize static variables in java.
5.what is static block in java?

  • Class loading time itself these variables gets memory
  • Static methods are the methods with static keyword are class level. without creating the object of the class we can call these static methods.

    1. public static void show(){ 
    2.  
    3. }

  • Static block also known as static initializer
  • 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.  }
6. write a java program to assign static variables using static block


  1. package com.staticInitializer;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. static{
  7.       System.out.println("First static block executed");
  8. }
  9.  
  10. static{
  11.      System.out.println("Second static block executed");
  12. }
  13.  
  14. static{
  15.      System.out.println("Third static block executed");
  16. }
  17.  
  18. }
     
Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed

  1.Static Variables 

  2.Static Methods 

  3.Static Blocks

  4.Main method

Read: Java Static Constructor : is it possible to create static constrcutor?

Topic: Evolution of Systems Integration Previous Topic   Next Topic Topic: Java Equals Hashcode Contract Example

Sponsored Links



Google
  Web Artima.com   

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