Java Assignment 2
Java Assignment 2
Do the required modification in the following snippet if any and then state the output:
class Demo
{
static {
System.out.println("Welcome to the code snippet....");
}
void method1() {
System.out.println("method 1");
}
void method2() {
System.out.println("method 2");
method3();
}
static void method3() {
System.out.println("method 3");
}
static void method4() {
System.out.println("method 4");
method1();
}
public static void main(String[] args) {
Demo demo = new Demo();
demo.method2();
method1();
Demo.method3();
demo.method4();
}
}
Solution-package snippet.java;
class Demo
{
static {
System.out.println("Welcome to the code snippet....");
}
static void method1() {
System.out.println("method 1");
}
void method2() {
System.out.println("method 2");
method3();
}
static void method3() {
System.out.println("method 3");
}
static void method4() {
System.out.println("method 4");
method1();
}
public static void main(String[] args) {
Demo demo = new Demo();
demo.method2();
method1();
Demo.method3();
Demo.method4();
}
}
Windows PowerShell
at snippet.java.Demo.main(demo.java:26)
PS C:\Users\USER> & 'C:\Program Files\Java\jdk-18.0.2.1\bin\
java.exe' '-
agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=local
host:62015' '--enable-preview' '-XX:
+ShowCodeDetailsInExceptionMessages' '-cp' 'C:\Users\USER\
AppData\Local\Temp\vscodesws_07318\jdt_ws\jdt.ls-java-project\
bin' 'snippet.java.Demo'
Welcome to the code snippet....
method 2
method 3
method 1
method 3
method 4
method 1
PS C:\Users\USER>