LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-28-2006, 02:58 AM   #1
chief_officer
Member
 
Registered: Mar 2006
Location: Istanbul, TR
Distribution: Red Hat, CentOS, Ubuntu
Posts: 180

Rep: Reputation: 30
Java: non-static variable this cannot be referenced from a static context


Friends,

I am trying to learn Java through some books and tutorials and I have run into an error message that I couldn't solve. I have a conceptual misunderstanding. Here is my code:

Code:
package javaapplication5;


public class Main 
{
    

class Box
{
    int width, height, depth; 

    Box(int w, int h, int d)
    {
        width = w;
        height = h;
        depth = d;
    }

    int volume()
    {
        return width * height * depth;
    }
    
}
    
    public Main() 
    {
    }


    public static void main(String[] args) 
    {
        int vol;
        Box mybox1 = new Box (10, 20, 15);
        Box mybox2 = new Box (3, 6, 9);
        
        vol = mybox1.volume();
        System.out.println("Volume is " + vol);
        
        vol = mybox2.volume();
        System.out.println("Volume is " + vol);
    }
    
}
The messages are as follows:

Code:
init:
deps-jar:
Compiling 1 source file to /home/diablo/java_codes/JavaApplication5/build/classes
/home/diablo/java_codes/JavaApplication5/src/javaapplication5/Main.java:34: non-static variable this cannot be referenced from a static context
        Box mybox1 = new Box (10, 20, 15);
/home/diablo/java_codes/JavaApplication5/src/javaapplication5/Main.java:35: non-static variable this cannot be referenced from a static context
        Box mybox2 = new Box (3, 6, 9);
2 errors
BUILD FAILED (total time: 0 seconds)
So, what I didn't understand is that, I am creating a simple class, a box and I want to give its dimensions and calculate the volume in public static void main(String[] args). However, it says that the variable is non-static (ok, my class is not a static variable) and this cannot be referenced from a static context. Here is the tricky part that I don't understand: I have to set length, width and depth of the box to make some calculations and the values of these three variables must be static. How can it be possible to set -say- length = 10 in a *not* static way?

Then I thought, maybe the *static* keyword in public static void main(String[] args) header is causing headache. I removed the static keyword in the header and it compiled without errors. However, no output is produced.

I think it has nothing to do with the *static* keyword in the main header and I am not understanding the concept here. Can anyone point to my *obvious* mistake?

If this helps, I am running Netbeans 5.0 IDE on SuSE 9.3 Pro box.

Thank you all for your kind assistance as always.

Regards,
 
Old 05-28-2006, 03:39 AM   #2
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 50
change class Box by static class Box
 
Old 05-28-2006, 04:23 AM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
or you can put class Box in a separate file called "Box.java", which is what one normally does with classes. What you did here is you made Box a part of Main, which is kind of weird and may not be what you intended.

Last edited by spooon; 05-28-2006 at 04:24 AM.
 
Old 05-28-2006, 12:27 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Actually, I think this is really what you probably want:
Code:
public class Box
{
    int width, height, depth;

    Box(int w, int h, int d)
    {
        width = w;
        height = h;
        depth = d;
    }

    int volume()
    {
        return width * height * depth;
    }

    public static void main(String[] args)
    {
        int vol;
        Box mybox1 = new Box (10, 20, 15);
        Box mybox2 = new Box (3, 6, 9);

        vol = mybox1.volume();
        System.out.println("Volume is " + vol);

        vol = mybox2.volume();
        System.out.println("Volume is " + vol);
    }
}
This gives the following:
Quote:
javac Box.java
java Box
Volume is 3000
Volume is 162
The source file will need to be changed to "Box.java".
 
Old 05-29-2006, 01:37 PM   #5
chief_officer
Member
 
Registered: Mar 2006
Location: Istanbul, TR
Distribution: Red Hat, CentOS, Ubuntu
Posts: 180

Original Poster
Rep: Reputation: 30
Thank you all for your kind attention and assistance.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto Assign Multiple Static Public IP Addresses under SBC's PPPoE Static Ip system o trekgraham Linux - Networking 8 04-17-2007 10:51 AM
C++: difference between static method variable and static class variable? Thinking Programming 3 01-16-2006 10:08 AM
extern static variable alaios Programming 9 09-11-2005 04:11 AM
What is a static variable? saiz66 Programming 7 09-30-2004 05:17 PM
FC2 Overriding static if in favor of dhcp system set for static pkraus109 Linux - Networking 8 09-21-2004 11:13 AM


All times are GMT -5. The time now is 11:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration