Solved

Compilation error

Posted on 2002-03-03
8
195 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 50 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nextBoolean(double p) for Random class 3 41
going to wrong jsp page 2 33
Java basic valueOf question 1 29
java mysql insert application 14 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

830 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