Results 1 to 4 of 4
- 12-12-2008, 10:34 AM #1
Member
- Join Date
- May 2008
- Posts
- 1
- Rep Power
- 0
According to java why can not this program be compiled?
public class Lab1
{
static Person pers;
public static void main (String [] args){
//Lab1.Person pers = new Lab1().new Person();
pers=new Person();
pers.setFirstName("Bob");
pers.setLastName("Fields");
pers.printOut();
tester();
pers.printOut();
}
static void tester(){
Lab1.Person pers =new Lab1().new Person();
pers.setFirstName("Homer");
pers.setLastName("Simpson");
pers.printOut();
}
class Person
{
String firstName;
String lastName;
public void setFirstName(String newFirstName)
{
firstName=newFirstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String newLastName)
{
lastName=newLastName;
}
public String getLastName(String newFirstName)
{
return lastName;
}
//Behavior
public void printOut()
{
System.out.println("Name: " + firstName + " " +
lastName );
}
}
}
The above program can not be compiled the error message is "non-static variable this cannot be referenced from a static context
pers=new Person();" The problem I have in mind is pers is declared as an static variable and it is assigned an exact Person obect so even if it is an static variable it can find it's way to the to the Object, so even if things are this way according to my view why it can not be compiled
Varuna:confused:
- 12-12-2008, 10:57 AM #2
Member
- Join Date
- May 2008
- Posts
- 2
- Rep Power
- 0
Re: According to java why can not this program be compiled?
Please visit forum4everyone.com to get the solution.
Technical -> Java -> General
- 12-12-2008, 03:40 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
- 12-12-2008, 03:44 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
You already had the solution in there. Unless there is a reason you uncommented this line?
Java Code:Lab1.Person pers = new Lab1().new Person();
Java Code:public class Lab1 { //static Person pers; public static void main(String[] args) { Lab1.Person pers = new Lab1().new Person(); //pers = new Person(); pers.setFirstName("Bob"); ...
Similar Threads
-
Java Program for doing FTP
By Rajesh_J2EE in forum New To JavaReplies: 1Last Post: 12-06-2008, 03:35 AM -
Using compiled Classes
By Mindhunter74 in forum NetBeansReplies: 17Last Post: 12-02-2008, 10:15 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 03:40 PM -
New to Java Program
By jvasilj1 in forum New To JavaReplies: 1Last Post: 02-05-2008, 08:22 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 10:33 PM
Bookmarks