0% found this document useful (0 votes)
65 views

Java Assignment 2

This Java code snippet defines a Demo class with several static and non-static methods. The main method creates a Demo object, calls some methods on it, and calls static methods directly. The code was modified to make method1 static to resolve a compilation error. When run, it outputs the text from each method call in order.

Uploaded by

Sourav Debnath
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)
65 views

Java Assignment 2

This Java code snippet defines a Demo class with several static and non-static methods. The main method creates a Demo object, calls some methods on it, and calls static methods directly. The code was modified to make method1 static to resolve a compilation error. When run, it outputs the text from each method call in order.

Uploaded by

Sourav Debnath
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/ 3

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

Try the new cross-platform PowerShell https://aka.ms/pscore6

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:61996' '--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....
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
Cannot make a static reference to the non-static method
method1() from the type Demo

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>

You might also like