Results 1 to 2 of 2

Thread: Static class

  1. #1
    eva
    eva is offline Member
    Join Date
    Dec 2007
    Posts
    49
    Rep Power
    0

    Default Static class

    Can a class be static? My idea is to declare a class whose attributes and methods can be accessed without creating its object.

    - PEACE

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default

    A class cannot be static, but for you purposes you can set the attributes and methods to be static. This way you can access it without create an instance.

    Java Code:
    public class Constant {
        public static final String ERROR = "1";
    
        public static String calculate(int number, int number) {
            //
        }
    }
    
    public class Test {
        public static void main(String... args) {
            String error = Constant.ERROR;
            Constant.calculate(1, 10);
        }
    }

Similar Threads

  1. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 12:20 AM
  2. Replies: 0
    Last Post: 04-16-2008, 11:07 PM
  3. Mocking static methods of class
    By Kat in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 12:24 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •