Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26

Thread: Static fields

  1. #21
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

  2. #22
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default may be this is not the correct thread but still..

    Hi,
    I am using this approach!

    is there any speed overhead:-

    Java Code:
    public interface myInterface{
       public interface another1{
            public interface another2{
                   pubic static String myVar1 = "var1";
                   pubic static String myVar2 = Utils.createName ( myVar1 );
            }
       }
    
       public interface another11{
            public interface another12{
                   pubic static String myVar11 = "var11";
                   pubic static String myVar12 = Utils.createName ( myVar11 );
            }
       }
    
    }
    then accessing like:
    Java Code:
    myFunction ( myInterface.another1.another2.myVar1 );
    Java Code:
    public interface myInterface{
       pubic static String myVar1 = "var1";
       pubic static String myVar2 = Utils.createName ( myVar1 );
       pubic static String myVar11 = "var11";
       pubic static String myVar21 = Utils.createName ( myVar11 );
       
       }
    then accessing:

    Java Code:
    myFunction ( myInterfacemyVar1 );

    ?? which one is preferrable , along with time complexity??? Please help
    i am the future

  3. #23
    sparlay_pk is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    How can we synchronize a static method, in order to protect it from parallel accesses
    ..
    Last edited by sparlay_pk; 08-22-2009 at 03:34 PM.

  4. #24
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    It seems to me that the first method
    ClassName.myNumber
    makes significantly more sense. Instantiating every time you want to change/get myNumber requires a lot more work. Also, its much easier to pick out that its a static field.

  5. #25
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by sparlay_pk View Post
    How can we synchronize a static method, in order to protect it from parallel accesses
    ..
    You er, just synchronize it with the synchronized keyword.

  6. #26
    Join Date
    Aug 2009
    Posts
    7
    Rep Power
    0

    Default

    In my personal opinion, static methods and fields should be accessed by MyClass.myMethodOrField, as it clearly states that the method/field is static (as long as you conform to the "object names start with lowercase" standard). Furthermore, if you access it from an object, you could come back later and forget that it's static, modify it's value, and then end up with nasty little bugs.

    P.S. Nice topic, I love these discussions :-P

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 01:35 AM
  2. Replies: 0
    Last Post: 04-17-2008, 12:07 AM
  3. Jsp / Java cannot get fields
    By Dracos in forum New To Java
    Replies: 0
    Last Post: 02-04-2008, 10:39 PM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10: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
  •