The Artima Developer Community
Sponsored Link

Java Answers Forum
Needing an analogy

2 replies on 1 page. Most recent reply: Jun 30, 2002 12:59 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

Needing an analogy Posted: Jun 28, 2002 12:16 PM
Reply to this message Reply
Advertisement
Can anyone set my single tracked, straightlined mind on the correct path to understanding java? I am trying to understand what I am missing when it comes to creating an object within PUBLIC STATIC VOID MAIN. then I get an error saying NON-STATIC VARIABLE suchAndSuch CANNOT BE REFERENCED FROM A STATIC CONTEXT. Is there anyone there that can explain this perhaps in the context of a theoretical analogy as to why I cannot do this.


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Needing an analogy Posted: Jun 28, 2002 8:59 PM
Reply to this message Reply
Static refers to class variables, methods. What that means is if you have a method which is public static, then the method can be accessed without creating an object of the class of which it is a part. So, coming to accessing a variable from static method, if the variable is non static, that means an object of that class is required to access the variable. Since you are not creating any object and are trying to access the variable directly, the reference to that variable is not allowed.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Needing an analogy Posted: Jun 30, 2002 12:59 AM
Reply to this message Reply
Hey, where's the analogy!?

How's this: main() is like your day: you wake up, stumble into your kitchen and fumble through the cupboard for your favorite coffee cup. The coffee cup is like a reference to an object. It is not of much use unless you put something in it. So you create some coffee and put it in the cup. Coffee is the (important) object and the cup is your means of access to it.

public class AnotherDay
{
   public static void main( String [] args )
   {
      // Zzz...
      Java coffeeCup = null;  // The cup is empty.
      coffeeCup = new Java(); // The cup is full.
      coffeeCup.takeAGulp();  // The cup is half full... or is it half empty?
   }
}
class Java
{
   int percentFull = 100;
 
   public void takeAGulp()
   {
      percentFull /= 2;
      System.out.println( "Gulp!  Ahh..." );
      System.out.println( "("+ percentFull + "% coffee left)" );
   }
}

(disclaimer: this is a very simplified analogy; had this been a real analogy, there would be many more objects, such as a cup object, a kitchen object, a coffee maker object, and on and on.)

Flat View: This topic has 2 replies on 1 page
Topic: Help with applet based on Mastermind game. Previous Topic   Next Topic Topic: WebSphere - Java Runtime?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use