Local classes - are those classes that are declared in a block of methods, if, for or any statements with a block besides a class. Normally, you can find a local class within the body of the method.
Local class has accessed to the members of the enclosing class. Note that it is the enclosing class where your method is declared.
It can access the local variables of that method as well. Note that local variable must be declare as final or effectively final. When a variable is final, you are not allowed to make changes to the value. Effectively final means that you did not explicitly specified keyword final in the variable declaration and the compiler can distinguish that you did not make any changes to it.
Local class has an accessed to the method parameters. Unlike the local variables, there is no need to declare it as final.
Take note of naming of variables. It is better to avoid naming the variables with the same name. If this happens, the rules for shadowing of variables applies to local classes as well.
Other rules of Local classes
1. A local class cannot be declared private or static. Hence just like an inner class, it cannot declare a static member. A local class is non static so it can accessed the members of the enclosing class.
2. Local classes defined inside a static method can only access a static member of the enclosing class because the method is static. But if the local class was defined in a non static method, then, you will have no problem in accessing the members of the enclosing class.
=============================
class PracticeMay23 {
private int studentId;
private static int idCounter = 0;
public PracticeMay23(){
++idCounter;
studentId += idCounter;
}
private void processName(String fn, String mn, String ln, boolean withSuffix){
final String suffixJr = "Jr.";
class ProcessingNameHere{
String getProcessName(){
if(withSuffix){
return fn + " " + mn + " " + ln + " " + suffixJr;
}else {
return fn + " " + mn + " " + ln;
}
}
void displayName(){
System.out.println("id: " + studentId);
System.out.println("name: " + getProcessName());
}
}
ProcessingNameHere p = new ProcessingNameHere();
p.displayName();
}
public static void main(String[] args){
PracticeMay23 p1 = new PracticeMay23();
p1.processName("Bell", "Cranel", "Myth", true);
PracticeMay23 p2 = new PracticeMay23();
p2.processName("Moshisuki", "Toya", "SmartPhone", false);
}
}=========================Results:id: 1name: Bell Cranel Myth Jr.id: 2name: Moshisuki Toya SmartPhone
Rules 3, 4 and 5 are to be verified in the succeeding lessons in oracle tutorials.
3. A local class can only have static members if it is declared as a constant member. A constant member happens only in variable declaration. So, if you declare a static method, it will cause an error.
Example: (constant variable declaration)
private static final double PI = 3.14;
4. You cannot declare an interface inside a block because an interface is inherently static.
5. You cannot declare a static initializer or a member interface in a local class.
Revising the example above to test Rule 3, 4 and 5
========================
class PracticeMay23 {
private int studentId;
private static int idCounter = 0;
public PracticeMay23(){
++idCounter;
studentId += idCounter;
}
private void processName(String fn, String mn, String ln, boolean withSuffix){
final String suffixJr = "Jr.";
class ProcessingNameHere{
String getProcessName(){
if(withSuffix){
return fn + " " + mn + " " + ln + " " + suffixJr;
}else {
return fn + " " + mn + " " + ln;
}
}
void displayName(){
System.out.println("id: " + studentId);
System.out.println("name: " + getProcessName());
}
static void displaying(){
System.out.println("Display below");
System.out.println("-----------------");
}
interface AddingNumbers{
double add(double a, double b);
}
class TryingInterfaceHere implements AddingNumbers{
@Override
public double add(double a, double b) {
return a + b;
}
}
}
ProcessingNameHere p = new ProcessingNameHere();
ProcessingNameHere.displaying();
p.displayName();
ProcessingNameHere.TryingInterfaceHere t = p.new TryingInterfaceHere();
System.out.println(t.add(3.14, 5.50));
}
public static void main(String[] args){
PracticeMay23 p1 = new PracticeMay23();
p1.processName("Bell", "Cranel", "Myth", true);
PracticeMay23 p2 = new PracticeMay23();
p2.processName("Moshisuki", "Toya", "SmartPhone", false);
}
}=============================Result:Display below-----------------id: 1name: Bell Cranel Myth Jr.8.64Display below-----------------id: 2name: Moshisuki Toya SmartPhone8.64
Note:
The interface and the static method that is not a constant declared within a block works just fine!!! I tested it inside the method as well and it works just fine!!!
YOU ARE READING
Java Programming
Randomhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...