What's the equivalent of C's “static” keyword in Java?

前端 未结 5 1688
粉色の甜心
粉色の甜心 2020-12-05 12:32

I want to know what could be the equivalent keyword in java which could perform same function as \"Static keyword in C\".. I want to do recursion in java, performing same fu

5条回答
  •  情书的邮戳
    2020-12-05 13:16

    You can simulate a static class in java as follows:

       /**
         * Utility class: this class contains only static methods and behaves as a static class.
         */
    
    public abstract class Utilities
        {
            // prevent inheritance
            private Utilities()
            {
            }
        // ... all your static methods here
            public static Person convert(string) {...}
        }
    

    This class cannot be inherited (like final because although abstract it has a private constuctor), cannot be instantiated (like static because abstract) so only static methods in it can be called.

提交回复
热议问题