Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Static variable

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    Mumbai, India
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Static variable

    Consider my code:
    public static Socket sock=new Socket();
    public static Socket recvsock;
    public static SocketInfo ipinfo;



    public static void setSocket(Socket socket)
    {

    sock=socket;
    Log.d(handsOn.Tag, ""+sock.getInetAddress());
    }
    public static Socket getSocket()
    {
    Log.d(handsOn.Tag, "sock"+sock.getInetAddress());
    return sock;
    }


    I m trying to make get n set function to a static variable... I m able to set .. but not get it getting a null value ... ??


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I set a static variable??

    What variable has a null value when what line is executed? Can you post the full text of the error message?

    Does the code call set before it calls get?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Location
    Mumbai, India
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I set a static variable??

    Yes i did set it first and i m getting "sock's inet address. In set method... But same variable gives null in get function.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I set a static variable??

    Is some other code setting the variable directly? Change the attribute to private to keep any other code from changing it directly.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Static variable

    Please don't hijack a post that is two years old. I have moved your post to its own thread.

Similar Threads

  1. putting a non static variable in a static variable
    By beennn in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 23rd, 2012, 05:06 PM
  2. Replies: 4
    Last Post: November 15th, 2012, 12:09 AM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  4. non-static variable
    By frozen java in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 15th, 2011, 06:18 PM
  5. non-static variable, addActionListener()
    By bobbyrne01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 10:58 AM