Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Compilation error

Posted on 2002-03-03
8
Medium Priority
?
200 Views
Last Modified: 2010-08-05
Hi!
Does anyone know what this compilation error means?

"StudentRegister.java:14: non-static variable this cannot be referenced from a static context
                                Car c = new Car();"
                                        ^


(Car is a second class in my program)
                                           
0
Comment
Question by:Cat75
8 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6837406
Can you please post code of the section of your StudentRegister class that causes the problem?
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6837420
Sorry, can you please show the code of your Car class (not the StudentRegister class), especially the constructor if you have it?
0
 
LVL 2

Expert Comment

by:cyberfrank
ID: 6837432
The problem is that you can not use "this" variable in the static method. Static means static content for the all objects instantiated for the given class, so there is no meaning of "this". The "this" variable can be only used inside of non-static method of the instantiated object.
I am sure, that You try to call some static method from the Car() constructor and that static method used "this" variable.

BR,

Cyberfrank
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:daitt
ID: 6837576
I think the problem might be that Car is an inner class and can be constructed only with a this object. You put the code to construct Car in a static method, which cannot refer to "this".
For example:
class Main{
class Car{
}
public static void test(){
Car c = new Car();
}
}

I still don't understand why the error started at c, not Car(). Maybe it's a different problem. Post your code, please.
Just guessing,
daitt
0
 

Accepted Solution

by:
Senthilb earned 200 total points
ID: 6838502

Hi

U r not shown the full program

But my guess is

1. u may defined Car as Inner class
2. check out u have default constructor for "Car" class
3. Move u r "Car" class description before creating object.



0
 
LVL 3

Expert Comment

by:daitt
ID: 6838605
Senthilb:
1. I said about this already in my comment.
2. This will cause "cannot resolve symbol" error, not the error Cat75 said.
3. This is Java, not C. The class declaration can be anywhere.

Cat75: what is the answer for your question?
0
 

Author Comment

by:Cat75
ID: 6838875
Car was an inner class. I chose the explanation that I understood the best. Sorry :/
0
 
LVL 3

Expert Comment

by:daitt
ID: 6839048
Are you sure you understood the problem?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question