5

Here is my snippet of code :

public class Test {

    public static void main(String[] args) {

        class EnglishHelloThere {
              int a=10;
        }
    }

}

I created a local class to see what kind of access modifier do i get when not written any modifier for variable in local class from compiler.

This is what i got in JAVAP

  Compiled from "Test.java"
class com.Test$1EnglishHelloThere
  SourceFile: "Test.java"
  EnclosingMethod: #21.#23                // com.Test.main
  InnerClasses:
       #27= #1; //EnglishHelloThere=class com/Test$1EnglishHelloThere
  minor version: 0
  major version: 51
  flags: ACC_SUPER
Constant pool:
   #1 = Class              #2             //  com/Test$1EnglishHelloThere
   #2 = Utf8               com/Test$1EnglishHelloThere
   #3 = Class              #4             //  java/lang/Object
   #4 = Utf8               java/lang/Object
   #5 = Utf8               a
   #6 = Utf8               I
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Methodref          #3.#11         //  java/lang/Object."<init>":()V
  #11 = NameAndType        #7:#8          //  "<init>":()V
  #12 = Fieldref           #1.#13         //  com/Test$1EnglishHelloThere.a:I
  #13 = NameAndType        #5:#6          //  a:I
  #14 = Utf8               LineNumberTable
  #15 = Utf8               LocalVariableTable
  #16 = Utf8               this
  #17 = Utf8               Lcom/Test$1EnglishHelloThere;
  #18 = Utf8               SourceFile
  #19 = Utf8               Test.java
  #20 = Utf8               EnclosingMethod
  #21 = Class              #22            //  com/Test
  #22 = Utf8               com/Test
  #23 = NameAndType        #24:#25        //  main:([Ljava/lang/String;)V
  #24 = Utf8               main
  #25 = Utf8               ([Ljava/lang/String;)V
  #26 = Utf8               InnerClasses
  #27 = Utf8               EnglishHelloThere
{
  int a;
    flags:

  com.Test$1EnglishHelloThere();
    flags:
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: invokespecial #10                 // Method java/lang/Object."<init>
":()V
         4: aload_0
         5: bipush        10
         7: putfield      #12                 // Field a:I
        10: return
      LineNumberTable:
        line 12: 0
        line 13: 4
        line 12: 10
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0      11     0  this   Lcom/Test$1EnglishHelloThere;
}

So basically flags field is left blank so i'm confused what kind of access modifier does this variable get

because if i add private int a=10; or public int a=10; i get

public int a;
  flags: ACC_PUBLIC

or

protected int a;
  flags: ACC_PROTECTED

So what kind of access modifier does a gets by default ?

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
  • 3
    possible duplicate of [What is the default access modifier in java?](http://stackoverflow.com/questions/16164902/what-is-the-default-access-modifier-in-java) – Morfic Apr 23 '15 at 10:51
  • It gets no access modifiers by default, note you can't add `private` or `protected` to a local variable. You can only make it `final`. – Peter Lawrey Apr 23 '15 at 10:51

3 Answers3

1

The default - when no modifier is present - is package private (aka package local). Which restricts the visibility to within the same package. That is irrespective of where the class is defined (top-level, inner, anonymous or method local).

That said, the visibility of fields of method local classes is not very relevant, as they are limited to the scope of the method.

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
  • 1
    @PeterLawrey The question is about a field in a method-local class. That said, the visibility of fields of method local classes is not very relevant, as they are limited to the scope of the method. – Mark Rotteveel Apr 23 '15 at 10:53
1

The following table shows the access to members permitted by each modifier.

enter image description here

But this access modifier can be confusing "Note the term Permitted here".It's worth noting that Local inner classes are local to code blocks From this i mean to say that Local inner classes also called method local inner classes are not members of the class of which the code is a part but are local to the code block they belong to, just as a local variable.

It can't be accessed outside the block in which they are defined.

For Example Suppose i read write this code in one package

package packageone.com;
public class HavingLocalClass {
    public HavingLocalClass() {
        // TODO Auto-generated constructor stub
    }
     void TestMethod(int a){
        class LocalClass{
            int localVar;
            public void display(){
            System.out.println(localVar);
            }

            public LocalClass(int localVar) {
                this.localVar=localVar;
                // TODO Auto-generated constructor stub
            }



            }

            LocalClass lc=new LocalClass(a);
            System.out.println(lc.localVar);
            /* If i directly try to print localVar here it will
                give me error */
            //System.out.println(localVar);//cannot be resolved into variable


    }
}

now in the same package if i try to print i have to access it through that method only.

package packageone.com;

public class TestingMain {
public static void main(String...strings){
HavingLocalClass tsp= new HavingLocalClass();
tsp.TestMethod(85);
}
}

output 85

Ankur Anand
  • 3,699
  • 1
  • 21
  • 39
0

It will get default/package-private modifier if not specified with any other modifier.Also a local variable can't be assigned with any other modifier(protected,public or private) but default.Also it can be added with final to restrict its value change.For more reference

Community
  • 1
  • 1
Narendra Pandey
  • 369
  • 1
  • 8
  • 25