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

Practice Programs - Classes

Notes that are in detail and useful

Uploaded by

inboxinsta0987
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)
23 views5 pages

Practice Programs - Classes

Notes that are in detail and useful

Uploaded by

inboxinsta0987
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

public class Charat {

public static void main(String[] args) {


String str = "Hello";
char ch = str.charAt(1);
System.out.println(ch);
}
}

public class Continuelab {

public static void main(String[] args) {


int I = 0;
outer:
while (true)
{
I++;
inner:
for (int j = 0; j < 10; j++)
{
I += j;
if (j == 3)
continue inner;
break outer;
}
continue outer;
}
System.out.println(I);
}
}

public class ContinueLabel {

public static void main(String[] args) {


outer: for (int i=0; i<10; i++) {
for(int j=0; j<10; j++) { if(j > i) {
System.out.println(); continue outer;
}
System.out.print(" " + (i * j));
}
}
System.out.println();
}
}
class Country {

// Static variable
static int countryCounter;

String name;
int dummyCounter;

public static void main(String[] args) {


// Creating first instance
Country ob1 = new Country();

// Assigning values to object's data variables.


ob1.name = "India";
ob1.dummyCounter = 1;

++ob1.countryCounter;

// Creating second instance of the class


Country ob2 = new Country();
ob2.name = "france";
ob2.dummyCounter = 1;

++ob2.countryCounter;

System.out.println("ob1.countryCounter = " + ob1.countryCounter);


System.out.println("ob2.countryCounter = " + ob2.countryCounter);
System.out.println("Country.countryCounter = " + Country.countryCounter);
}
}

public class InstaVar {


public String student; // Declared Instance Variable
InstaVar()
{ // Default Constructor
this.student= "Yogananda"; // initializing Instance Variable
}

public static void main(String[] args) {


// TODO Auto-generated method stub
// Object Creation
InstaVar name = new InstaVar();
System.out.println("Student name is: " + name.student);

}
public class LocalVar {

public static void main(String[] args) {


// TODO Auto-generated method stub
int var = 89; // Declared a Local Variable

// This variable is local to this main method only

System.out.println("Local Variable: " + var);


}
}

public class PrintArray {


public static void main(String[] args) {
// TODO Auto-generated method stub
String[] array = { "hi", "hello", "java"};

for(String str : array) {


System.out.println(str);
}
}
}

class StaticVariableDemo {
static int staticVar = 40; // Static variable

public void display() {


System.out.println("Static variable value: " + staticVar);
}

public static void main(String[] args) {


StaticVariableDemo obj1 = new StaticVariableDemo();
StaticVariableDemo obj2 = new StaticVariableDemo();

obj1.display(); // Output will be 40

StaticVariableDemo.staticVar = 50; // Changing staticVar using class name


obj2.display(); // Output will be 50 for both instances
}
}
public class StatVar {
public static String student= "Yatish"; //Declared static variable

public static void main(String[] args) {


// TODO Auto-generated method stub
//student variable can be accessed without object creation
System.out.println("Student Name is : "+StatVar.student);

class Abc {
int val;
Abc() {
System.out.println("Default Constructor Called");
}
Abc(int x)
{
//this();
val=x;
System.out.println("Param Constructor " +val);
}
}
class ThisEx {
public static void main(String args[]){
Abc obj=new Abc(10);
}
}

class Student {
String name;
int age;

// Constructor with parameters using 'this' to refer to instance variables


public Student(String name, int age) {
this.name = name;
this.age = age;
}

// Method to display student details


public void display() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}

// Method to demonstrate 'this' can be used to call another method


public void showDetails() {
this.display(); // 'this' refers to the current object's method
}
}

public class Main {


public static void main(String[] args) {
// Creating an object of Student class
Student student1 = new Student("Alice", 21);

// Calling the method using the 'this' keyword


student1.showDetails();
}
}

You might also like