Skip to main content

Posts

Constructor in Java

Constructor in Java constructor in Java  is a special member method which will be called implicitly (automatically) by the JVM (JAVA VIRTUAL MACHINE) whenever an object is created for placing user or programmer defined values in place of default values. In a single word constructor is a special member method which will be called automatically whenever object is created. The purpose of constructor is to initialize an object called object initialization. Constructors are mainly create for initializing the object. Initialization is a process of assigning user defined values at the time of allocation of memory space. Syntax className () { ....... ....... } Advantages of constructors in Java A constructor eliminates placing the default values. A constructor eliminates calling the normal or ordinary method implicitly. How Constructor eliminate default values ? Constructor are mainly used for eliminate default values by user defined values, whenever we create an ob...
Difference between Static and non-static method in Java In case of non-static method memory is allocated multiple time whenever method is calling. But memory for static method is allocated only once at the time of class loading. Method of a class can be declared in two different ways Non-static methods Static methods Difference between non-static and static Method Non-Static method Static method 1 These method never be preceded by static keyword Example: void fun1 () { ...... ...... } These method always preceded by static keyword Example: static void fun2 () { ...... ...... } 2 Memory is allocated multiple time whenever method is calling. Memory is allocated only once at the time of class loading. 3 It is specific to an object so that these are also known as instance method. These are common to every object so that it is also known as member method or class method. 4 These methods always access with object reference Syntax: Objref.methodname(); T...