From 6b9647add407d02b7f7baaa123be2084ea007161 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Sat, 14 Sep 2024 01:56:07 +0530 Subject: [PATCH] Upload files to "OOP/Java/Lab/Week7/ExampleQues" --- OOP/Java/Lab/Week7/ExampleQues/Example1.java | 14 ++++++++++ OOP/Java/Lab/Week7/ExampleQues/Example2.java | 17 ++++++++++++ OOP/Java/Lab/Week7/ExampleQues/Example3.java | 20 ++++++++++++++ OOP/Java/Lab/Week7/ExampleQues/Example4.java | 28 ++++++++++++++++++++ OOP/Java/Lab/Week7/ExampleQues/Example5.java | 15 +++++++++++ 5 files changed, 94 insertions(+) create mode 100644 OOP/Java/Lab/Week7/ExampleQues/Example1.java create mode 100644 OOP/Java/Lab/Week7/ExampleQues/Example2.java create mode 100644 OOP/Java/Lab/Week7/ExampleQues/Example3.java create mode 100644 OOP/Java/Lab/Week7/ExampleQues/Example4.java create mode 100644 OOP/Java/Lab/Week7/ExampleQues/Example5.java diff --git a/OOP/Java/Lab/Week7/ExampleQues/Example1.java b/OOP/Java/Lab/Week7/ExampleQues/Example1.java new file mode 100644 index 0000000..4ceb014 --- /dev/null +++ b/OOP/Java/Lab/Week7/ExampleQues/Example1.java @@ -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 diff --git a/OOP/Java/Lab/Week7/ExampleQues/Example2.java b/OOP/Java/Lab/Week7/ExampleQues/Example2.java new file mode 100644 index 0000000..f82222c --- /dev/null +++ b/OOP/Java/Lab/Week7/ExampleQues/Example2.java @@ -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); + } +} diff --git a/OOP/Java/Lab/Week7/ExampleQues/Example3.java b/OOP/Java/Lab/Week7/ExampleQues/Example3.java new file mode 100644 index 0000000..d303009 --- /dev/null +++ b/OOP/Java/Lab/Week7/ExampleQues/Example3.java @@ -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 diff --git a/OOP/Java/Lab/Week7/ExampleQues/Example4.java b/OOP/Java/Lab/Week7/ExampleQues/Example4.java new file mode 100644 index 0000000..e35d4a8 --- /dev/null +++ b/OOP/Java/Lab/Week7/ExampleQues/Example4.java @@ -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: expected +// block static{ +// ^ diff --git a/OOP/Java/Lab/Week7/ExampleQues/Example5.java b/OOP/Java/Lab/Week7/ExampleQues/Example5.java new file mode 100644 index 0000000..b9c6c69 --- /dev/null +++ b/OOP/Java/Lab/Week7/ExampleQues/Example5.java @@ -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