Results 1 to 6 of 6

Thread: Java - static field problem

  1. #1
    Join Date
    Dec 2009
    Posts
    292

    Java - static field problem

    Hello,
    I use static field and I have java.lang.NoClassDefFoundError occurring. I have two classes C1 and C2 (in my case, they are not in the same package).
    Code:
    class C1
    {
    static String fld = "...";
    }
    
    class C2
    {
    static String fld = "..."+C1.fld;
    }
    The java.lang.NoClassDefFoundError occurs on C2. I have the impression that the field of C2 can be initialized only if the class C1 has been charged. Is this true? How impossibility to classloader order on classes? I watched from the side of import static but without success. If you have any idea about this then please help me. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Java - static field problem

    Hello,
    This requires that it works declare variables "public" if only classes that override these classes can access the attributes.
    class C1
    {
    Public static String fld = "...";
    }
    I think this is a better than the one which you have used in your code. I hope this will help you.

  3. #3
    Join Date
    Nov 2009
    Posts
    335

    Re: Java - static field problem

    Hello,
    I do not think that your attribute is static is something to be neither its initialization. You have just a problem of visibility. In addition to the public modifier as said in the above post, you do "import to the C1 object, "Because I do not think that alone is enough public and your problem should be solved. I hope you are understanding what I am trying to explain you.

  4. #4
    Join Date
    Nov 2009
    Posts
    343

    Re: Java - static field problem

    Hello,
    I agree with the above post. The concept of visibility should not be linked to this error message. In addition, it seems that Java everything is public by default. The default visibility of a method in a java class is not "public." In fact this kind of method is public for all classes for the same package. If you need more information on this then please do visit the Sun's official site and there you can find detailed information on this.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Java - static field problem

    Hello,
    I think the "java.lang.NoClassDefFoundError" is a runtime error ... It is not my problem meaning or import of visibility (which would produce errors at compile time) but a problem of classpath. I think your code is not really the same as default attributes have visibility <package-view> and are therefore not visible from two different packages. And it would be nice to have the complete trace of the exception.

  6. #6
    Join Date
    Dec 2009
    Posts
    292

    Re: Java - static field problem

    Hello,
    Thank you for your answers. As stated in the above post, my problem is runtime and I have understood that. I have no problem with visibility or import. I will explore the trail of the classpath but I really do not know where to start. And one more question I would like to ask that what we have to do to eliminate the runtime error.

Similar Threads

  1. What is static method in java?
    By Vaibhav S in forum Software Development
    Replies: 5
    Last Post: 25-09-2011, 09:40 AM
  2. Dynamic and Static Polymorphism in Java
    By Ardent in forum Software Development
    Replies: 4
    Last Post: 19-08-2010, 03:37 PM
  3. Make a static reference to the non-static field
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 11:11 AM
  4. What is static import feature in java?
    By Juany in forum Software Development
    Replies: 4
    Last Post: 01-02-2010, 07:00 PM
  5. Java example for static members
    By ScarFace 01 in forum Software Development
    Replies: 6
    Last Post: 05-01-2010, 05:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,483,925.55724 seconds with 16 queries