File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Could some one explain whats happening here. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Could some one explain whats happening here." Watch "Could some one explain whats happening here." New topic
Author

Could some one explain whats happening here.

Tony Combe
Greenhorn

Joined: Mar 14, 2002
Posts: 17
Hi
I'm having a bit of a problem understanding whats happening in the code below. The first application runs fine but the next one comes up with a "non-static blahBlah variable can not be accessed from a static context". The wierd thing is if I create an instance of PrintNum in PrintNum main method and then access the object ShowNum like this "PrintNum.ShowNum.thing" then everything works fine.

***** FIRST PROGRAM
public class PrintNum
{
public static void main(String[] args)
{
ShowNum blahBlah = new ShowNum();
System.out.println(blahBlah.thing);
}
}

****** SECOND PROGRAM
public class PrintNum
{
ShowNum blahBlah;
public PrintNum()
{
blahBlah = new ShowNum();
}
public static void main(String[] args)
{
ShowNum blahBlah = new ShowNum();

System.out.println(blahBlah.thing);

}
}
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
By making blahBlah an instance variable within the second PrintNum, you make it only accessible to objects of PrintNum. Since main is a static method, it can't access instance variables without having a reference to a PrintNum object.
So when you create a PrintNum object, you are now able to use that reference to access blahBlah.
If you make ShowNum blahBlah static, then your main can access it fine.
In the first example, you have no instance variables, and hence main doesn't have a problem.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Well, since you don't have the declaration for ShowNum(), I'm not sure if it contains a member called "thing" or not, so I will just assume it does.
Your posted code for the second program is:

(BTW, please use code tags, as it makes your code more readable.)
The only way I can see to get the error you are producing is if in fact the code is actually:

In the second case (which is NOT the code you posted), you are now referring to a class variable named "blahBlah". Since you're making the reference in main(), which is static, you will get the error you describe. But if you re-define blahBlah as you do in your posted code as in #1 above, you are shadowing the class variable by declaring a local variable with the same name. In this case, it would compile, since the local variable is in scope of the method when you make the reference.


Rob
SCJP 1.4
Tony Combe
Greenhorn

Joined: Mar 14, 2002
Posts: 17
Firstly thanks for your help Jason and Rob.
Secondly when rob said I have no instance variables in no1 example what would the definition of the variable blahBlah be?. I'm sorry to ask what must seem like a stupid question but from what I've heard from other sources so far the only difference in the definition (as in the code not what it really is) between a class and an instance variable is that a class variable has static in front of it. Oh and while I'm asking stupid questions I might as well one more what does code tags mean?
Thanks all
Tony
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
When you create a post, there a buttons below the "Add Reply" button that say "URL", "BOLD", "QUOTE", ..., "CODE".
Whenever you want to place code in your post. Click on that "CODE" button. Doing so will cause a couple of UBB tags to appear in your post. Put your code in between those tags so that it will be formatted in a way that everyone can read it easily.
Hope to see you around here often!
Corey


SCJP Tipline, etc.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Tony
In the first code piece of code you post the variable blahBlah is a local variable in the main method. It is not an instance variable.
In the second section of code the ist blahblah variable is an instance variable, the second blahBlah you declare (in main) is again a local variable.
What Rob is saying is that the second pice of code you posted does not produce an error (providing that the ShowNum class has a member 'thing'). The only way to get the error you describe is if you try to access a non-static variable, in a static context.
In Robs #2 example he took off the ShowNum from the declaration of the blahBlah variale in your main method. By doing that the line now accesses the original declaration of blahBlah (the instance variable) and tries to have it refer to a new instance of a ShowNum object. This will cause the error you described.
The way your second piece of code is posted you wouldn't get the error because you are just creating a local variable and that is not a problem in a static method.
hope that helps
And, by the way, dont apologize for asking questions that is what most people are here to do, and we dont mind answerig them if theya re well asked and not just slapped together or cut and pasted code from some other site. And dont forget to do a search for answers too.


Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could some one explain whats happening here.