I am told there are three different contexts in which a class can be declared in Java.
It has to do with the location within a program, but I can't think of what they are.
Obviously a class can be declared at the top of a page, the only other example I can think of is like a nested class?
I feel I may be going about this the wrong way.
In a package
package com.example.mypackage;
public class TheClass {
}
In a class
package com.example.mypackage;
public class OuterClass {
class InnerClass {
}
}
Anonymously
public class MainClass {
public static void main(String[] args) {
AbstractClass myObject = new AbstractClass() {
// overrides and other fields of the
// anonymous class goes in this block
};
}
}
EDIT: As @daniu stated in the comment, a class can also be created in a method:
public class MainClass {
public static void main(String[] args) {
class MethodClass {
}
MethodClass myObject = new MethodClass();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With