0% found this document useful (0 votes)
46 views4 pages

Name Roll - No Reg - No: Public Class Int Int Static Int Int

The document contains 4 Java programs that demonstrate the use of static variables and methods. The first program shows how a static variable "section" can be accessed by multiple objects. The second program explains some key properties of static methods like they belong to the class and not objects. It also shows a static method incrementing a static variable. The third program increments a static variable from multiple objects. Finally, the fourth program calculates the volume of a cuboid using a static height variable and takes the radius as a command line argument.

Uploaded by

Sahil Kishore
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)
46 views4 pages

Name Roll - No Reg - No: Public Class Int Int Static Int Int

The document contains 4 Java programs that demonstrate the use of static variables and methods. The first program shows how a static variable "section" can be accessed by multiple objects. The second program explains some key properties of static methods like they belong to the class and not objects. It also shows a static method incrementing a static variable. The third program increments a static variable from multiple objects. Finally, the fourth program calculates the volume of a cuboid using a static height variable and takes the radius as a command line argument.

Uploaded by

Sahil Kishore
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/ 4

//1st Programs -----------------

//Constructor using static variables


//created by Sahil Kishore on 23/08/2019 @9:50AM
public class ConstrStatic {
String name;
int roll_no;
int reg_no;
static String section="D1E13" ;
ConstrStatic(String x,int y,int z)
{
name=x;
roll_no=y;
reg_no=z;
}

void display() {
System.out.println("Name = "+name +" , Roll no = "+roll_no+" , reg_no
="+reg_no+" ,Section"+section);
}

public static void main(String[] args) {


ConstrStatic obj1=new ConstrStatic("Sahil",40,11709120);
ConstrStatic obj2=new ConstrStatic("Yashi",20,11708032);
obj1.display();
obj2.display();
}
}
//------------------------------------------------

//2nd programs-------------------------------
//Static Method program NOTES
/*
* 1.Static Methods belong to the entire class rather than their objects.
* 2.They can be accessed without creating objects.
* 3.They can access only static data members.
*/
public class staticMethod {
static int count=10; //note:-place static or test without static
int x;

staticMethod(int y) //creating a constructor


{
x=y;
}

static void increment() //Static member function


{
count++;
System.out.println(count);
}
void display() //member functions
{

System.out.println(x);
}

public static void main(String args[])


{
staticMethod.increment();
staticMethod obj1=new staticMethod(100);
staticMethod obj2=new staticMethod(200);
obj1.display();
obj2.display();
staticMethod.increment();

}
//--------------------------------------------------------------

//Program 3 ---------------------------------------

//Use of Static
// 23/08/2019
public class useOfStatic {

static int count=0; //program with or without static


void display()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
useOfStatic ob1=new useOfStatic();
useOfStatic ob2=new useOfStatic();
useOfStatic ob3=new useOfStatic();
ob1.display();
ob2.display();
ob3.display();

}
}
/* Advantages of Static------
1.single copy is created and used by all objects. Ex.Sections of class
*/
//Program 4----------------------

/*

* 1.Calculate Volume of Cuboid = πr²h ,where h should be static and

* value of r will be given using command line argument.

*/

import java.util.Scanner;

public class volumeCuboid {

double pi=3.14;

static double h=2;

double r,vol;

void calculate(int r) {

vol=pi*(r*r)*h;

System.out.println("Volume of the cuboid is "+vol);

public static void main()

int radi;

volumeCuboid v=new volumeCuboid();

Scanner rad=new Scanner(System.in);

System.out.println("Enter the value of radius - ");

radi=rad.nextInt();

v.calculate(radi);

/*

* Next PROGRAMS to be created:


* 1.Create volume of cuboid , cube, sphere, cylinder ,hemisphere ,cone using different

* cases such as -

* (i).Command line arguments

* (ii).Scanner class

* (iii).Switch Case

* (iv).do while

* (v).static varible

* (vi).static memberfunction

* (vii).Constructor

* THURSDAY LAB EVALUATION - 29 /08/2019

* ZD Softscreen recorder 9.1 - install for lab evaluation(Compulsory)

*/

You might also like