0% found this document useful (0 votes)
48 views5 pages

create A Simple Static Method With A Constructor in Java

The document discusses static methods, variables, and blocks in Java. It provides examples of creating static methods with and without constructors, differentiating static and non-static methods, creating multiple static methods, creating static variables of different types, and comparing static and non-static variables. The examples demonstrate how static methods and variables can be accessed without creating an object and how their values are shared across object instances.

Uploaded by

gjkumar_mca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views5 pages

create A Simple Static Method With A Constructor in Java

The document discusses static methods, variables, and blocks in Java. It provides examples of creating static methods with and without constructors, differentiating static and non-static methods, creating multiple static methods, creating static variables of different types, and comparing static and non-static variables. The examples demonstrate how static methods and variables can be accessed without creating an object and how their values are shared across object instances.

Uploaded by

gjkumar_mca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

//Create a Simple Static method with a Constructor in Java

public class Static_Ex0


{

static
{

System.out.println("The Static");
}

Static_Ex0()
{

System.out.println("The Constructor");
}
}

class MainClass
{

public static void main(String args[])


{

new Static_Ex0();
}
}

//Output

The Static
The Constructor

//Differentiate Static method with a normal method in Java


public class Static_Ex3
{
static
{
System.out.println("The Static");
}

Static_Ex3() {

System.out.println("The Constructor");
}
void Method() {

System.out.println("The Method");
}
}
class MainClass {

public static void main(String args[])


{

Static_Ex3 obj = new Static_Ex3();

obj.Method();
}
}

//Output

The Static
The Constructor
The Method

//Create Multiple Static methods in Java


public class Static_Ex2
{

static
{
System.out.println("The Static");
}

Static_Ex2()
{
System.out.println("The Constructor");
}

void Method()
{
System.out.println("The Method");
}
}

class MainClass
{

static
{
System.out.println("The MainClass Static");
}
public static void main(String args[])
{
Static_Ex2 obj = new Static_Ex2();
obj.Method();
}
}

//Output

The MainClass Static


The Static
The Constructor
The Method

//Create Static variables in Java

public class Static_Ex1


{

static int a = 10;


static float b = (float) 20.63;
static double c = 123.4567890123;
static boolean d = true;
static char e = 'A';
static String f = "SampleCodez";

Static_Ex1() {

System.out.println("The Static Variables are : \n");


System.out.println("The int value is : " + a);
System.out.println("The float value is : " + b);
System.out.println("The double value is : " + c);
System.out.println("The boolean value is : " + d);
System.out.println("The char value is : " + e);
System.out.println("The String value is : " + f);
}
}

class MainClass {

public static void main(String args[])


{

new Static_Ex1();
}
}

//Output
The Static Variables are :

The int value is : 10


The float value is : 20.63
The double value is : 123.4567890123
The boolean value is : true
The char value is : A
The String value is : SampleCodez

//Static Variable vs Ordinary Variable in Java


public class Static_Ex
{
int i = 5;
static int j = 5;
void display()
{
System.out.println("int Variable : " + i);
System.out.println("Static int Variable : " + j);
i = i + 10;
j = j + 10;
}
}
class MainClass
{
public static void main(String args[])
{
Static_Ex obj1 = new Static_Ex();
Static_Ex obj2 = new Static_Ex();
obj1.display();
obj2.display();
}
}
//Output

int Variable : 5
Static int Variable : 5
int Variable : 5
Static int Variable : 15

You might also like