0

So, I'm beginning to learn Java and I think it's an awesome programming language, however I've come across the static keyword which, to my understanding, makes sure a given method or member variable is accessible through the class (e.g. MyClass.main()) rather than solely through the object (MyObject.main()). My question is, is it possible to make certain methods only accessible through the class and not through the object, so that MyClass.main() would work, however MyObject.main() would not? Whilst I'm not trying to achieve anything with this, I'd just like to know out of curiosity.

In my research I couldn't find this question being asked anywhere else, but if it is elsewhere I'd love to be pointed to it!

Forgive me if it's simple, however I've been thinking on this for a while and getting nowhere.

Thanks!

VortixDev
  • 965
  • 1
  • 10
  • 23
  • 3
    Short answer is no. – Dawood ibn Kareem Jul 22 '16 at 22:33
  • Fair enough. That was very quick, thanks! – VortixDev Jul 22 '16 at 22:33
  • 3
    Also see [Why isn't calling a static method by way of an instance an error for the Java compiler?](http://stackoverflow.com/questions/610458/why-isnt-calling-a-static-method-by-way-of-an-instance-an-error-for-the-java-co) – that other guy Jul 22 '16 at 22:36
  • While you can call a static method through an instance variable, even if its value is `null`, it is bad practice. Use the type name to call the method. – Lew Bloch Jul 22 '16 at 22:38
  • A good IDE will warn you if you try to access a static method using an instance variable. You *are* using an IDE, right? – Andreas Jul 22 '16 at 22:40
  • Usually I'd use Eclipse, but I'm away from my main PC for a bit so I'm sticking with javac, which doesn't warn me. – VortixDev Jul 22 '16 at 22:41

4 Answers4

2

Any static method or member belongs to the class, whereas non-static members belong to the object.

Calling a static method (or using a static member) by doing myObject.method() is actually exactly the same as MyClass.method() and any proper IDE will give a suggestion to change it to the second one, since that one is actually what you are doing regardless of which of the two you use.

Now to answer the actual question:

is it possible to make certain methods only accessible through the class and not through the object

No, not as far as i know, but like I said, any proper IDE will give a warning, since it makes little sense and it gives other readers of the code an instant hint that you're dealing with static members.

Olle Kelderman
  • 307
  • 4
  • 16
1

In Java, you can crete an object with these keywords.(new keyword, newInstance() method, clone() method, factory method and deserialization) And when you create an object,it can also use classes abilities which is like static methods.

Short answer:No.

berk geylani
  • 100
  • 6
1

Yes, short answer is no.

But you can put your static members in a dedicated class, so that no instances share any one of them.

MyObject is instance of MyClass, and you aggregate all you static parts in MyStaticThing.

Using static member on an instance can be misleading, so it is a bad practice

http://grepcode.com/file/repo1.maven.org/maven2/org.sonarsource.java/java-checks/3.4/org/sonar/l10n/java/rules/squid/S2209.html

While it is possible to access static members from a class instance, it's bad form, and considered by most to be misleading because it implies to the readers of your code thatthere's an instance of the member per class instance.

Another thing, do not use static things, because you cannot do abstraction and replace implementations to extend your code.

Being able to switch between implementations is useful for maintenance and tests.

chikincrow
  • 393
  • 3
  • 11
1

Is it possible to make certain methods only accessible through the class and not through the object?

Yes, it is. You achieve this by preventing any instances of the class to ever be created, by making the class non-instantiable: declare its constructor private.

public final class NonInstantiable {

    private NonInstantiable() {
        throw new RuntimeException(
              "This class shouldn't be instantiated -- not even through reflection!");
    }
     /* static methods here... */
 }

Now, it only makes sense to declare any methods of the class static -- and they can only be called through the class name. Such a class is often called a utility class.

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30