Upload files to "OOP/Java/Lab/Week7/ExampleQues"

This commit is contained in:
Aadit Agrawal 2024-09-14 01:56:07 +05:30
parent 3c665489ed
commit 6b9647add4
5 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,14 @@
class Example1 {
//Static class
static class X {
static String str = "Inside Class X";
}
public static void main(String args[]) {
X.str = "Inside Class Example1";
System.out.println("String stored in str is- " + X.str);
}
}
// Output: String stored in str is- Inside Class Example1

View File

@ -0,0 +1,17 @@
class Example2{
int num;
//Static class
static class X{
static String str="Inside Class X";
num=99;
// static int num = 99;
// would be the correct syntax
}
public static void main(String args[]){
Example2.X obj = new Example2.X();
System.out.println("Value of num="+obj.str);
}
}

View File

@ -0,0 +1,20 @@
class Example3 {
static int num;
static String mystr;
static {
num = 97;
mystr = "Static keyword in Java";
}
public static void main(String args[]) {
System.out.println("Value of num=" + num);
System.out.println("Value of mystr=" + mystr);
}
}
// Last } wasn't included, but I added it.
// Output:
// Value of num=97
// Value of mystr=Static keyword in Java

View File

@ -0,0 +1,28 @@
class Example4{
static int num;
static String mystr;
//First Static
block static{
System.out.println("Static Block 1");
num = 68;
mystr = "Block1";
}
//Second static
block static{
System.out.println("Static Block 2");
num = 98;
mystr = "Block2";
}
public static void main(String args[]){
System.out.println("Value of num="+num);
}
}
// Error:
// Example4.java:6: error: <identifier> expected
// block static{
// ^

View File

@ -0,0 +1,15 @@
class Example5 {
static int i;
static String s;
public static void main(String args[]) { //Its a Static Method
Example5 obj = new Example5();
//Non Static variables accessed using object obj
System.out.println("i:" + obj.i);
System.out.println("s:" + obj.s);
}
}
//Output:
//i:0
//s:null