简体   繁体   中英

Why use public and private to static members and method?

I'm learning Java and I just wonder why public and private is used when a method or members is static? When static is used they are class methods and class members and could be used from other classes without creating an object, so is public and private necessary? Some help is preciated to understand. Sorry if this question is too simple for some.

The accessibility of a field or method is orthogonal to the fact that it's static or not.

You could have a static method accessible from the outside, and a static method that must only be used from inside the class itself (by other static or non-static methods). The same goes for fields.

For example:

// not visible from the outside
private static final long MILLISECONDS_IN_A_MINUTE = 1000L * 60 * 60;

public static Date addMinutes(Date d, int amount) {
    return addMillis(d, MILLISECONDS_IN_A_MINUTE * amount);
}

// not visible from the outside
private static Date addMillis(Date d, long amount) {
    return new Date(d.getTime() + amount);
}

I'm learning Java and I just wonder why public and private is used when a method or members is static?

I believe your question is due to a common misconception that the access modifiers are for instances , but they're not!

Two different instances can access each others private members if they are of the same class .

In other words, the access modifiers works on class level . Since also static members belong to some class, it makes sense to have access modifiers also on them.

A static method (or variable) that should only be used by code in the same class (as in the example by JB Nizet) should be private, while a static method or variable that may be used by code in any class should be public.

It's not necessary, but there can be static methods and data members for internal use only.

An example for this is if you want an unique id for every instance of the class:

class Foo
{
   private static int nextId = 0;
   private static int generateId() { return ++nextId; }
   private int id;
   public Foo()
   {
      id = generateId();
   }
}

As you can see, nextId and generateId() are not needed outside the class, nor should they be used outside the class. The class itself is responsible for generating id's. But you need them to be static (well, you need nextId to be static , but you can also make generateId() static since it doesn't access non-static members).

Whenever an object Foo is created, the static counter is incremented, thus you get different ids for each instance of the class. (this example is not thread-safe)

Suppose you have a static public method and this method must access to a private attribute. This private attribute must be static too. There's one reason why private static exists.

Example :

public class Test {
  private static int myattr = 0;

  public static void foo() {
    myattr = 2;
  }
}

Above, myattr must be a static attribute in order to use it in the foo() method.

Yes it is needed.

If you have a Static Method and want to use a private variables in that method, then you need to declare it static too.

Or you want the static variables not be visible to other packages, then don't declare it public.

From what I remember, it's not really needed . But public means, basically in any programming language, that it can be used by outside files. With private it can only be used within that file, and static means you cannot change the value of said reference. Whether these be functions, or variables, the same rules apply. I might be off. Haven't done Java in about a year and a half.

The ways you can incorporate these types is up to you. After all, a program is only as diverse as it's user. ^_^

Public and private keywoards have to do with visibility: which members do you want to accessible to other classes and which should be hidden or encapsulated. Static members relate to the class as a whole, while non-static members operate on object instances.

When the static is used with methods it doesn't only mean that it should be used by the members of other classes.

The case when we access the static methods of a class is one when the class (which contains the method) cannot be instantiated ie no objects can be created of that class .

There may be situations when two different classes may have static methods with same name. In that case you want to use the method of the same class not the method of other class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM