Solved

Compilation error

Posted on 2002-03-03
8
194 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 57
varialbe initialization 11 33
servlet example issue 6 40
What's wrong with this code? 4 23
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

831 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