- passive income - passive income

Static technique in Java Programming

Static technique in Java Programming

Sometimes you wish to use category members while not the utilization of reference to a specific instance. ordinarily, category members should be accessed mistreatment object, but u will produce category members which will be employed by itself while not the utilization of specific instance, you'll be able to try this by declaring a member as a static. once a member is asserted static, it will be accessed before any objects of its category square measure created, and while no relation to any object. each technique and variables will be declared as static. The common example of the static technique is main() technique it's declared as static as a result of it should be known as before any object exist.

public static void main(String[] args)

when member declared as a static solely single copy of it’s is employed by all the objects.

static variables
Now 1st see what happens once you declare any variable as static by a program

class StaticDemo 

public static void main(String args[])


The output of on top of the program is:




So as you'll be able to see the worth of I ought to be one whenever builder is known as however i is asserted as static therefore solely single copy of i is shared by all 3 objects.
Static technique  in Java Programming

Why declare any variable as static?

let us see through associate degree example

Suppose their square measure five hundred students in my school, currently, all instance knowledge members can get memory every time once an object is formed. All student has its distinctive roll no and name, therefore, instance knowledge member is nice. Here, school refers to the common property of all objects. If we tend to build it static, this field can get memory one time.

class Student{

int rollno;

String name;

static String school =" ITS”;

Student(intr, String n)

void show ()

public static void main(String args[])


static strategies

Methods declared as static have many restrictions:

• they'll solely directly decision alternative static strategies.

• they'll solely directly access static knowledge.

• they can't consult with this or super in any method.

// Demonstrate static variables, methods, and blocks.

class static 


public static void main(String args[]) 


In on top of programs we tend to use static variables, static strategies and static block and once category is loaded all static statements can run before the other statements 1st a can have worth three then static block can run and b can have a*4 that's twelve and so main technique are known as that calls meth( ), passing forty two to x. The 3 println()statements consult with the 2 static variables a and b, furthermore on the native variable x. Here is that the output of the program:

Static block initialized.

x = 42

a = 3

b = 12

One last purpose static strategies and variables will be used outside category severally, without the utilization of object u simply ought to use dot operator once category name following square measure general kind for mistreatment static member outside the category

classname.method( );

Static technique in Java Programming Static technique  in Java Programming Reviewed by Raj Aryan on July 04, 2019 Rating: 5


Powered by Blogger.