包级访问中是否包含默认包?

时间:2014-12-16 12:20:17

标签: java

我想知道如何在Java中定义默认包。我知道如何定义公共和私有访问,但我不知道是否存在在java中的包级别访问中定义的任何默认包访问。 / p>

我试图执行的代码是:

  class A
  {
    public static void value()
    {
    int a;
    a=5;
    }
    public static void main()
    {
    value();
    }
  }
  class B
  {
    public void greet()
    {
    System.out.println("Value of a is"+a);
    }
  }

我得到的错误是:

D:\Downloads\pro>javac A.java
A.java:17: error: cannot find symbol
System.out.println("Value of a is"+a);
                                   ^
  symbol:   variable a
  location: class B
1 error

既然这两个类都属于同一个默认包,那么B类不应该访问A类的成员(a)吗?

我问这个问题是因为当我编译包含两个类的java文件,因为没有为类提供修饰符时,java编译器会给包级访问作为类的默认访问修饰符。因为没有定义包,java编译器会使用默认包但我无法知道java中的包级别访问是否包含默认包。任何人都可以帮助我。

6 个答案:

答案 0 :(得分:2)

a是静态函数value中的一个变量,在该函数之外根本不可见。与访问说明符无关。

答案 1 :(得分:1)

默认包是没有名称的包,文件顶部没有包声明的所有类都属于它。

除了您不能从包内的类引用它的类之外,它受所有包的正常规则约束。

例如,我有2个java文件:

public class A{
    public static void foo(){System.out.println("fooCalled");}
}

package com.example;

public class B{
    public static void main(String[] arg){
        A.foo();//won't compile
    }

}

然后B(或以限定格式com.example.B)永远不会在没有reflection魔法的情况下调用A的foo。

答案 2 :(得分:1)

您的程序与access specifiers无关,因为您已在方法中声明了变量int a

因此它变成了local variable。你甚至不能在同一个类中使用此方法。

如果我们专门讨论访问说明符,那么我们可以在Java中使用default访问说明符,其范围仅限于同一个包。

package com;
class A{
    int a; // this is an instance variable
    static int b; //this is a class variable
}

package com;
class B{
    //can use a variable here
    // To use a here, we need new A().a;
    // To use b here, we can do,  A.b
}

package hello;
class C{
    //can't use a variable here
}

修改

假设我们在MyProgram.java上创建名为Desktop的文件。以下是此文件的代码

class First{
    int a; // a is an instance variable
    static int b;  // b is a static (class) variable

    void display(){
        int c; // c is a local variable
    }
}

class Second{
    public static void mian(){
        First obj = new First();
        obj.a = 10;  // to access instance variable we need object of the class
        obj.b = 20;  // class variable can also be accessed using the object

        // First.a = 10;  //It won't work as a is instance variable and can be accessed by object only

        // First.b = 20;  // We can also access static variables by class name directly without using any object

        // obj.c = 30;  // It won't work. As c is a local variable of method display and can be used only inside that method.

        // First.c = 30;  //It also won't work as c can only be used inside the method where it is declared.
    }
}

答案 3 :(得分:1)

所以最后我得到了如何在不创建对象的情况下访问另一个类中的变量,只要两个类都放在同一个包中。只需要变量 static

以下是代码:

import java.io.*;
  class A
  {
  static int a=5;
    public void turn()
    {
    System.out.println("value of a is"+a);
    }
  }
  class B
  {
    public static void main(String args[])
    {
    int b;
    b=A.a;
    System.out.println("value of a is"+b);
    }
  }

这表明驻留在同一个包中的类可以访问彼此的成员,前提是它是静态的,即使它不是公共的,因为默认包访问来玩。

答案 4 :(得分:0)

Default access modifier means we do not explicitly declare an access modifier for a class, field, 
method, etc.

A variable or method declared without any access control modifier is available to any other    
class    in the same package. The fields in an interface are implicitly public static final and 
the methods in an interface are by default public.

答案 5 :(得分:0)

class B中,a未定义。

class B {
    public void greet() {
        System.out.println("Value of a is" + a );//cannot find `a` inside `B`
    }
}

现在可访问性,因为您的两个类在同一个包B类中可以访问其他类的A类的公共,受保护和默认(无访问修饰符)成员。但在您的情况下,aA成员方法value()中的局部变量,因此无法在声明它的方法之外访问a变量。

class A {
    public static void value() {
       int a;//not accessible outside this method
       a=5;
    }
    public static void main() {//FYI: this main is not valid to execute your code, missing here: `String[] args` argument
       value();
       //`a`, is even not accessible here, forget about class `B`
    }
}

示例代码,都在同一个包中:

class A {
   String bar;
}

class B {
  public foo() {
    A a = new A();//required, as `bar` is instance(non-static) member of class `A`
    a.bar 'hi there';//set value
    System.out.printf("a.bar = %s\n", a.bar);
  }
}

修改

嵌套类的示例代码:

class A {
  int foo;

  class B {
     void setFoo() {
       foo = 45; //accessing member of class `A`
     }
  }
}

工作代码:

<pre>
<code>
class A {
  private int foo;
  private B b;

  A() {
  	foo = -1;
  	b = new B();
  }

  class B {
     void setFoo(int foo) {
     	System.out.printf("Inside B's setFoo(), foo = %d\n", foo);
        A.this.foo = foo; //accessing member of class `A`
     }
  }

  int getFoo() {
  	return foo;
  }

  public void setFoo(int foo) {
  	System.out.printf("Inside A's setFoo(), foo = %d\n", foo);
  	b.setFoo(foo);
  }
}

class Ideone{
	public static void main (String[] args)	{
		A a = new A();
		System.out.printf("main(), foo = %d\n", a.getFoo());
		a.setFoo(34);
		System.out.printf("main(), foo = %d\n", a.getFoo());
	}
}
</code>
</pre>