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: private static variables

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default private static variables

    What is the importance of having variables declared as "Private Static". Y can't we just have only private variables?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: private static variables

    You can have private variables. Please provide an example that demonstrates exactly what you're talking about.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: private static variables

    Public Class Test
    {
    private static int count=10;

    public void method()
    {
    }
    }

    Whats the importance of having private static variables?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: private static variables

    Quote Originally Posted by tcstcs View Post
    Whats the importance of having private static variables?
    What if that method is static and you don't want other classes to have access to the variable?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: private static variables

    Whats the importance of having private static variables?
    Making a variable private means that it is private, and making it static means that it is static.

    The importance of a variable being one or the other or both or neither depends on your code. You declare it to be the way you want it to be.

    -----

    Sorry for merely giving links. But twenty word summaries are apt be shallow (at best) or misleading (more likely). And it strikes me that you will make the most progress understanding the significance of these keywords if you google, read, and think a little about what they mean.

    If what you find is obscure, or strikes you as wrong, then post your ideas as they will form a good basis for a specific discussion.

  6. The Following User Says Thank You to pbrockway2 For This Useful Post:

    KevinWorkman (June 28th, 2012)

Similar Threads

  1. Non-Static variables
    By liloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 31st, 2010, 09:13 AM
  2. [SOLVED] paintComponent is not reading my private variables
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 25th, 2010, 02:31 PM
  3. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM
  4. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM