What does the flags mean for fields in class format in JAVA?

Here is my code snippet:

public class Test { public static void main(String[] args) { class EnglishHelloThere { int a=10; } } } 

I created a local class to find out which access modifier I get when no variable modifier compiles in the local class.

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 the flags field is left blank, so I got confused which access modifier makes this get variable

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 access modifier does by default?

+5
source share
3 answers

The following table shows access to members allowed by each modifier.

enter image description here

But this access modifier can be misleading, "Pay attention to the term permitted here." It should be noted that Local inner classes are local for code blocks. From this I want to say that Local inner classes, also called local inner classes of a method, are not members of the class of which the code is a part, but are local to the code block to which they belong as to a local variable.

It cannot be accessed outside the block in which they are defined.

For example, suppose you read 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 should access it only through this method.

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

pin 85

+1
source

The default value - in the absence of a modifier - is a private package (aka package local). This limits visibility within a single package. This is regardless of where the class is defined (top, inner, anonymous, or local method).

However, the visibility of the fields of the local classes of the method is not very relevant, since they are limited by the scope of the method.

+1
source

It will receive the default / package-private modifier if it is not specified by any other modifier. Also, a local variable cannot be assigned using any other modifier (protected, public or private), but by default. It can also be added using final to limit its price change. For more reference

0
source

All Articles