'How to use java .class file in Visual Studio Code?

I'm currently studying a Java course, and one of the assignments requires me to use (import) a class inside a .class file.

I'm currently using Visual Studio Code, and I can't seem to find a way to import the class into my Java code.

We are not given the .java code. From what I can tell, Visual Studio Code does not support the use of .class files.



Solution 1:[1]

as I know, intellij idea can decompile the .class file depends on the build in decompile plugin, while the extension named 'Java Decompiler' in vscode marketplace works a little complex. maybe you can take a try, use the 'Java Decompiler' extension or take a look in the intellij idea.

Solution 2:[2]

The easiest way is to have both classes in the same directory, like in this example. Say you have class A

public class A {
    public int fun() {
        return 0;
    }
}

to use class A from class B in the same directory, you just declare a type A in your B

class B {
    public static void main(String[] args) {
        A a = new A();
        int i = a.fun();
        System.out.println(i);
    }
}

this will print 0.

This works because both classes are in the same directory, and class A is declared public.

Being in the same directory, with no package declaration, means that both classes are in the default package, this is the easiest case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steven-MSFT
Solution 2 mico