0% found this document useful (0 votes)
2 views6 pages

Java Examples on Classes.docx

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

Java Examples on Classes.docx

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

1.

//Example on array runtime


import [Link];

public class Main

​ public static void main(String[] args) {

​ ​ [Link]("Hello World");

​ ​ int []arr=new int[5];

​ ​ Scanner sc=new Scanner([Link]);

​ ​ for(int i=0;i<5;i++)

​ ​ {

​ ​

​ ​ arr[i]=[Link]();

​ ​ }

​ ​ for(int i=0;i<5;i++)

​ ​ {

​ ​ [Link](arr[i]);

​ ​ }

[Link] on class and object


class Student {

// data member (also instance variable)

int id;
// data member (also instance variable)

String n;

public static void main(String args[]) {

// creating an object of Student

Student s1 = new Student();

[Link]([Link]);

[Link](s1.n);

[Link] class outside of main function


class Student{

int id;

String name;

class TestStudent2{

public static void main(String args[]){

Student s1=new Student();

[Link]=101;

[Link]="Sonoo";

[Link]([Link]+" "+[Link]);//printing members with a white space

[Link] method
public class MyClass {
public static void main(String[] args) {

// Initialize the object by reference variable in a single line

MyClass myObject = new MyClass();

// Access methods or fields of the object

[Link]();

public void myMethod() {

[Link]("Object initialized successfully!");

[Link] multiple objects


class Student{

int id;

String name;

class TestStudent3{

public static void main(String args[]){

//Creating objects

Student s1=new Student();

Student s2=new Student();

//Initializing objects

[Link]=101;

[Link]="Sonoo";

[Link]=102;
[Link]="Amit";

//Printing data

[Link]([Link]+" "+[Link]);

[Link]([Link]+" "+[Link]);

6. Creating class ,object and methods


class Student{

int rollno;

String name;

void insertRecord(int r, String n)

rollno=r;

name=n;

void displayInformation()

[Link](rollno+" "+name);}

class TestStudent4{

public static void main(String args[]){

Student s1=new Student();

Student s2=new Student();

[Link](111,"Karan");

[Link](222,"Aryan");
[Link]();

[Link]();

[Link] on constructor
import [Link].*;

// Driver Class
class Geeks {

// Constructor
Geeks()
{

[Link]("Constructor Called");
}

// main function
public static void main(String[] args)
{
Geeks geek = new Geeks();
}
}

8.

class Employee{

int id;

String name;

float salary;

void insert(int i, String n, float s) {

id=i;
name=n;

salary=s;

void display(){[Link](id+" "+name+" "+salary);}

public class TestEmployee {

public static void main(String[] args) {

Employee e1=new Employee();

Employee e2=new Employee();

Employee e3=new Employee();

[Link](101,"ajeet",45000);

[Link](102,"irfan",25000);

[Link](103,"nakul",55000);

[Link]();

[Link]();

[Link]();

You might also like