create A Simple Static Method With A Constructor in Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy