java modifier access_Java中的默认访问修饰符是什么? (What is the default access modifier in Java?)...

最佳答案

英文原文

If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning.

Full story you can read here (Which I wrote recently):

中文翻译

来自Java 文档

如果一个类没有修饰符(默认值,也称为包私有),它只在其自己的包中可见(包是相关类的命名组 - 您将在后面的课程中了解它们。)

在成员级别,你也可以像使用顶级类一样使用public修饰符或no modifier(package-private),并且具有相同的含义

你可以在这里阅读全文(我最近写的):

If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning.

Full story you can read here (Which I wrote recently):

来自Java 文档

如果一个类没有修饰符(默认值,也称为包私有),它只在其自己的包中可见(包是相关类的命名组 - 您将在后面的课程中了解它们。)

在成员级别,你也可以像使用顶级类一样使用public修饰符或no modifier(package-private),并且具有相同的含义

你可以在这里阅读全文(我最近写的):

参考答案2

Here is a code sample which should pretty much sum this up for you... In addition to the below, showing how you can't access a default in another package there is one more thing.

Default is not accessible in a subclass if the class that subclasses it is in another package, but it is accessible if the subclass is in the same package.

package main;

public class ClassA {

private int privateVar;

public int publicVar;

int defaultVar;

}

package main;

public class ClassB {

public static void main(String[] args) {

ClassA a = new ClassA();

int v1 = a.publicVar; // Works

int v2 = a.defaultVar; // Works

int v3 = a.privateVar; // Doesn't work

}

}

package other;

public class ClassC {

public static void main(String[] args) {

ClassA a = new ClassA();

int v1 = a.publicVar; // Works

int v2 = a.defaultVar; // Doesn't work

int v3 = a.privateVar; // Doesn't work

}

}

参考答案3

From documentation:

Access Levels

Modifier Class Package Subclass World

-----------------------------------------------------

public Y Y Y Y

protected Y Y Y N

(Default) Y Y N N

private Y N N N

参考答案4

The default modifier is package. Only code in the same package will be able to invoke this constructor.

参考答案5

It depends on the context.

When it's within a class:

class example1 {

int a = 10; // This is package-private (visible within package)

void method1() // This is package-private as well.

{

-----

}

}

When it's within a interface:

interface example2 {

int b = 10; // This is public and static.

void method2(); // This is public and abstract

}

参考答案6

Default access modifier is package-private - visible only from the same package

参考答案7

Your constructor's access modifier would be package-private(default). As you have declared the class public, it will be visible everywhere, but the constructor will not. Your constructor will be visible only in its package.

package flight.booking;

public class FlightLog // Public access modifier

{

private SpecificFlight flight;

FlightLog(SpecificFlight flight) // Default access modifier

{

this.flight = flight;

}

}

When you do not write any constructor in your class then the compiler generates a default constructor with the same access modifier of the class. For the following example, the compiler will generate a default constructor with the public access modifier (same as class).

package flight.booking;

public class FlightLog // Public access modifier

{

private SpecificFlight flight;

}

参考答案8

The Default access modifier is package-private (i.e DEFAULT) and it is visible only from the same package.

参考答案9

Yes, it is visible in the same package. Anything outside that package will not be allowed to access it.

参考答案10

Is the access modifier of this constructor protected or package?

I think implicitly your constructors access modifier would be your class's access modifier. as your class has public access, constructor would have public access implicitly

参考答案11

No, you can't call the default access level to the other package. But you have the access within the package. Follow this link for more details.

参考答案12

Default access modifier - If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes).

参考答案13

From a book named OCA Java SE 7 Programmer I:

The members of a class defined without using any explicit access

modifier are defined with package accessibility (also called default

accessibility). The members with package access are only accessible to

classes and interfaces defined in the same package.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值