Results 1 to 2 of 2
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default how to create static array in class

    Hi.
    I have (not static) class wiht static field.
    The int static filed are ok
    the problem is the static array - it just not complied, and i search and cannot find solution to why.
    please help
    I tried wih final, without, with "new String[]" , with "new String [4]" , without, but nothing work.


    Java Code:
    public class ViewFile
    	{
    		private File file;
    		private int fileType;
    		
    		public final static int otherT=0;
    		public final static int reoprtT=1;
    		public final static int runT=2;
    		public final static int statusT=3;		
                              public final  static String[] fileTypeS=new String[]
    		{"other", //0
    		"reoprt file", //1
    		"run file", //2
    		"Status file", //3
    		};
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    this seems to compile fine for me, though I would do it like so:
    Java Code:
    import java.io.File;
    
    public class ViewFile
    {
      private File file;
      private int fileType;
    
      public final static int otherT = 0;
      public final static int reoprtT = 1;
      public final static int runT = 2;
      public final static int statusT = 3;
      public final static String[] fileTypeS = 
      {
        "other", // 0
        "reoprt file", // 1
        "run file", // 2
        "Status file", // 3
      };
    }
    Though better still is to get rid of String constants and use enum instead:
    Java Code:
    import java.io.File;
    
    public class ViewFile
    {
      private File file;
      private int fileType;
    
      public enum FileTypes
      {
        OTHER(0), REPORT_FILE(1), RUN_FILE(2), STATUS_FILE(3);
        private int value;
        private FileTypes(int value)
        {
          this.value = value;
        }
        public int getValue()
        {
          return value;
        }
      }
    }
    Last edited by Fubarable; 04-19-2009 at 04:37 PM.

Similar Threads

  1. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 01:20 AM
  2. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-17-2008, 12:02 AM
  3. Please help, need to make my class static.
    By sumak in forum New To Java
    Replies: 1
    Last Post: 04-19-2008, 08:29 AM
  4. Static class
    By eva in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 02:02 PM
  5. Mocking static methods of class
    By Kat in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 01:24 AM

Posting Permissions

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