Results 1 to 8 of 8

Thread: Some questions

  1. #1
    Lundager is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Question Some questions

    Hi!

    I'm reading the "Killer Java Game Programming" book, and I was wondering why are these variables set to static?

    Java Code:
    private static final int PWIDTH = 500;   // size of panel
    private static final int PHEIGHT = 400;
    My other question is, what does the volatile keyword do, and why would you use it?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Some questions

    Quote Originally Posted by Lundager View Post
    Hi!

    I'm reading the "Killer Java Game Programming" book, and I was wondering why are these variables set to static?

    Java Code:
    private static final int PWIDTH = 500;   // size of panel
    private static final int PHEIGHT = 400;
    They are being declared as constants, and constants should be final and static -- final because they shouldn't be allowed to change, and static because the class only should have one instance of the constant.


    My other question is, what does the volatile keyword do, and why would you use it?
    Have you had a chance to look this up first? It's usually best to do a Google search, and then ask a question about what exactly you don't understand from your reading.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Some questions

    Quote Originally Posted by Lundager View Post
    I have tried to google the last question, but I didn't understand it very well.. so I thought I would ask here:)
    Again, can you elaborate on just what confuses you? You'll get a better more directed answer, and we won't have to waste our time re-writing complete tutorials that are already out there.

  4. #4
    Lundager is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Some questions

    Well I saw a tutorial where he declared a variable volatile like this:
    Java Code:
    private volatile boolean running = false;
    He said that the reason he used volatile was to avoid a huge crash later in the application?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Some questions

    Quote Originally Posted by Lundager View Post
    Well I saw a tutorial where he declared a variable volatile like this:
    Java Code:
    private volatile boolean running = false;
    He said that the reason he used volatile was to avoid a huge crash later in the application?
    Check out a better tutorial. Perhaps:
    this one
    or this one

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Some questions

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Lundager is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Some questions

    I don't understand this:

    What is the Java volatile keyword?

    Essentially, volatile is used to indicate that a variable's value will be modified by different threads.

    Declaring a volatile Java variable means:

    The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory";
    Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself.
    We say "acts as though" in the second point, because to the programmer at least (and probably in most JVM implementations) there is no actual lock object involved. Here is how synchronized and volatile compare:
    What is main memory, and synchronized blocks?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Some questions

    Synchronized blocks of code are code blocks that have limits on other code's ability to call such that only one thread can call the block at a time, to prevent threads from clashing.

    Threads have a local memory and can hold copies of variables that they are using (the cached thread-local variables). If the thread makes changes to this cached variable, it is not reflected in the original variable held in main memory (not in the thread's local memory), and so other objects that may be using that variable might not see changes in the variable caused by the first thread.

    Suggestion: read some tutorials on java thread as this can be pretty deep stuff. You'll use volatile on variables that are used by many threads and you want all threads to see changes to the variable as soon as they occur.

Similar Threads

  1. Help for some questions
    By king4oneday in forum Eclipse
    Replies: 1
    Last Post: 02-23-2012, 04:59 PM
  2. 3 questions
    By silverglade in forum New To Java
    Replies: 11
    Last Post: 05-09-2011, 03:17 PM
  3. Some Questions
    By MuslimCoder in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 04:01 PM
  4. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 08:08 PM
  5. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 07:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •