Compilation error

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)
                                           
Cat75Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SenthilbConnect With a Mentor Commented:

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
 
Peter KwanCommented:
Can you please post code of the section of your StudentRegister class that causes the problem?
0
 
Peter KwanCommented:
Sorry, can you please show the code of your Car class (not the StudentRegister class), especially the constructor if you have it?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
cyberfrankCommented:
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
 
daittCommented:
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
 
daittCommented:
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
 
Cat75Author Commented:
Car was an inner class. I chose the explanation that I understood the best. Sorry :/
0
 
daittCommented:
Are you sure you understood the problem?
0
All Courses

From novice to tech pro — start learning today.