Results 1 to 6 of 6

Thread: Make a static reference to the non-static field

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Make a static reference to the non-static field

    Hello,
    I used the log, the variable is not static what is normal
    Code:
    private Log l = LogFactory.getLog (TreeNodeMaintenance.class);
    I created a method that is static and I used this method in Log. What i am trying is to make a static reference to a non-static field. How can I do this in java, which method or what logic I have to use. Thanks in advance.
    Last edited by Aaliya Seth; 02-03-2010 at 11:13 AM.

  2. #2
    Join Date
    Nov 2009
    Posts
    335

    Re: Make a static reference to the non-static field

    Hello,
    In this case, you must also declare your static variable log, just have a look a the following syntax in your case
    Code:
    private static Log l = LogFactory.getLog (TreeNodeMaintenance.class);
    I hope you are understanding what I have tried to explain you here. If you have any more queries regarding this then you can post back and also remember to post with the error you get.
    Last edited by NetWorkInG; 02-03-2010 at 11:15 AM.

  3. #3
    Join Date
    Dec 2009
    Posts
    211

    Re: Make a static reference to the non-static field

    Hello,
    I think even I have a similar kind of a problem with this. My problem is that the project contain many small projects in it and each project uses a lot of class and all the sales person Log class. So if I change the variable is static log log I changed one of Development standards specified by the specifications of development. Any help on this is highly appreciated. Thanks in advance.

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: Make a static reference to the non-static field

    Hello,
    You have a design problem, you have a static method so common to all classes that you will call by a "myclass.method()" in which you want to access an instance variable (not static so) This is not possible and the compiler you say very well. Having used rarely logs (curiosity), I can not let you about this to use or not. Otherwise, rather than declaring log into class attribute, you can not create / recover within your static method? Finally, your method requires that it really be static.

  5. #5
    Join Date
    Dec 2009
    Posts
    213

    Re: Make a static reference to the non-static field

    Hello,
    I have one more query, what concerns the method must be static but I'll try to declare the log variable in static method. I think it is a solution and I'll do with the project manager very soon and if it is possible to make this change, it would be great. Till then if you have any other alternative for this the please reply back with it and if it fits my needs then I will use it.

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Make a static reference to the non-static field

    Hello,
    This is the general syntax of how you can do this, just and example or is a sample code and not the real code, below shown in just a syntax of what you will actually have in your code
    Code:
    public  class MyClass
    {
      public static void mystamet(MyClass o)
       {
        o.mynstamet();
      }
     
      public void mynstamet()
      {
      }
    }
    
    public class MyClass
    {
     public static void myMethod()
       {
       f();
      }
    
       public static void f()
      {
       }
    }

Similar Threads

  1. Java - static field problem
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 09:48 AM
  2. How to use UML in static model
    By Saura in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 12:05 AM
  3. Static IP for CCTv through DVR
    By CRReddy in forum Networking & Security
    Replies: 1
    Last Post: 02-02-2010, 05:32 AM
  4. Set Static IP In Ubuntu 9.10
    By REDBULL in forum Networking & Security
    Replies: 6
    Last Post: 10-01-2010, 03:36 PM
  5. Replies: 3
    Last Post: 25-04-2009, 11:34 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,711,717,877.83722 seconds with 16 queries