gpt4 book ai didi

java - 为什么不能直接访问hashmap静态变量

转载 作者:行者123 更新时间:2023-12-01 12:13:22 31 4
gpt4 key购买 nike

当我研究HashMap源代码时,我发现HashMap类有很多静态变量,例如:

public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
{
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 16;

/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30;

/**
* The load factor used when none specified in constructor.
**/
static final float DEFAULT_LOAD_FACTOR = 0.75f;

为什么我不能使用这些静态变量?

最佳答案

因为它们的访问级别是包,这意味着只有来自同一包的类才能访问它们。它们不适合公众使用。您可以阅读成员(member)访问规则详情here .

关于java - 为什么不能直接访问hashmap静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27150066/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com