back again with a compile error =(

Hey all!

I am trying to get this to compile but I keep getting a syntax error saying :

"No enclosing instance of type Lab4_1 is accessible. Must qualify the allocation with an enclosing instance of type Lab4_1 (e.g. x.new A() where x is an instance of Lab4_1)."

Maybe someone here can help me isolate the problem?

Well, here is the code (yes it is for class tomorrow =) )

import javax.swing.JOptionPane;

publicclass Lab4_1

{

publicclass Stock

{

String stockSymbol;

String stockName;

double previousClosingPrice;

double currentPrice;

public Stock()

{

this("defaultSym","defaultName" );

setCurrentPrice(0);

setPreviousClosingPrice(0);

}

public Stock(String symbol, String name)

{

setSymbol(symbol);

setName(name);

setCurrentPrice(65);

setPreviousClosingPrice(60);

}

public String getSymbol()

{

return stockSymbol;

}

public String getName()

{

return stockName;

}

publicdouble getPreviousClosingPrice()

{

return previousClosingPrice;

}

publicdouble getCurrentPrice()

{

return currentPrice;

}

publicvoid setSymbol(String symbol)

{

this.stockSymbol = symbol;

}

publicvoid setName(String name)

{

this.stockName = name;

}

publicvoid setPreviousClosingPrice(double price)

{

this.previousClosingPrice = price;

}

publicvoid setCurrentPrice(double price)

{

this.currentPrice = price;

}

publicdouble changePercent()

{

double percent = this.currentPrice - this.previousClosingPrice / this.currentPrice;

return percent;

}

}

publicstaticvoid main(String[] args)

{

Stock stock =new Stock("ESI","ITT Technical Institute Inc.");

String output ="Symbol: " + stock.stockSymbol +"\n Name: " + stock.stockName +"\n Previous Closing Price: " + stock.previousClosingPrice +"\n Current Price: " + stock.currentPrice +"\n Price Change: " + stock.changePercent();

JOptionPane.showMessageDialog(null, output);

}

}

Any help will be very much appreciated!

Sorry, I forgot to add that the "red sqiggly" points to the code:

new Stock("ESI", "ITT Technical Institute Inc.");

Message was edited by:

Blue_Chimera

[4667 byte] By [Blue_Chimeraa] at [2007-11-27 10:34:26]
# 1

Try changing this:

Stock stock = new Stock("ESI", "ITT Technical Institute Inc.");

to this

Stock stock = new Lab4_1().new Stock("ESI", "ITT Technical Institute Inc.");

petes1234a at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 2

> public class Stock

should be

static class Stock

hiwaa at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 3

The problem is that an inner class as you have defined it cannot stand on its own. It must be be part of an object of the outer class type. My example just declares an anonymouse outerclass so that you can hang your inner class on it.

petes1234a at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 4

> > public class Stock

> should be

> static class Stock

agree, static inner class would work too. Then it is behaving as an outer class.

Its drawback is that as a static inner class, it won't have access to the outer class's instance methods and variables (which is not a problem in your example).

Message was edited by:

petes1234

petes1234a at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 5

Or even better.

public class Stock

{

//...

}

public class Lab4_1

{

public static void main(String[] args)

{

//...

}

}

CaptainMorgan08a at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 6

> Or even better.

Captain Morgan is right, my bad: I answered the question; he fixed the underlying problem.

petes1234a at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 7

Well, When I put it like Capt morgan has it.. (which i tried before) it says that stock needs it's own file or class or something like that

Blue_Chimeraa at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 8

> Well, When I put it like Capt morgan has it.. (which

> i tried before) it says that stock needs it's own

> file or class or something like that

Yes it does. A .java file can only have one public class. So you should have 2 files:

Stock.java

Lab4_1.java

CaptainMorgan08a at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 9

I don't think I have learned how to link the two yet.. like in c++ you #include header.h etc...

would you mind explaining if it is a simple explaination?

BTW... I used Petes original suggesstion... however it looks funny :)

thanks!

Blue_Chimeraa at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 10

Just try putting them in separate files and compile them.

CaptainMorgan08a at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 11

> I don't think I have learned how to link the two

> yet.. like in c++ you #include header.h etc...

> would you mind explaining if it is a simple

> explaination?

> BTW... I used Petes original suggesstion... however

> it looks funny :)

> thanks!

My solution is a kludge, an inelegant solution that works but misses the real point. Cptn Morgan's is the real solution. I believe that it's all based on how Java is set up. It requires that separate classes be in separate files (except for internal classes, but you only use those for specific purposes).

petes1234a at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 12

OKI

will try that! Thank you both for the help! I will split the remaining two stars between you :)

Take care!

Blue_Chimeraa at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 13

or even better just one file

Stock.java

package cruft;

import javax.swing.JOptionPane;

public class Stock

{

String stockSymbol;

String stockName;

double previousClosingPrice;

double currentPrice;

public Stock()

{

this("defaultSym", "defaultName" );

setCurrentPrice(0);

setPreviousClosingPrice(0);

}

public Stock(String symbol, String name)

{

setSymbol(symbol);

setName(name);

setCurrentPrice(65);

setPreviousClosingPrice(60);

}

public String getSymbol()

{

return stockSymbol;

}

public String getName()

{

return stockName;

}

public double getPreviousClosingPrice()

{

return previousClosingPrice;

}

public double getCurrentPrice()

{

return currentPrice;

}

public void setSymbol(String symbol)

{

this.stockSymbol = symbol;

}

public void setName(String name)

{

this.stockName = name;

}

public void setPreviousClosingPrice(double price)

{

this.previousClosingPrice = price;

}

public void setCurrentPrice(double price)

{

this.currentPrice = price;

}

public double changePercent()

{

double percent = this.currentPrice - this.previousClosingPrice / this.currentPrice;

return percent;

}

public static void main(String[] args)

{

Stock stock = new Stock("ESI", "ITT Technical Institute Inc.");

String output = "Symbol: " + stock.stockSymbol + "\n Name: " + stock.stockName + "\n Previous Closing Price: " + stock.previousClosingPrice + "\n Current Price: " + stock.currentPrice + "\n Price Change: " + stock.changePercent();

JOptionPane.showMessageDialog(null, output);

}

}

fastmikea at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 14

fastmike

Again, spoonfeeding != teaching.

Again, you are not helping here, you are in fact harming the learning process.

Again, we are not impressed with your so-called coding abilities. Any one of us could just paste up the code and in fact create code far better than your ****, yet we know better.

Again, you are doing the forum a great disservice.

Please change your behavoir or just leave.

petes1234a at 2007-7-28 18:28:48 > top of Java-index,Java Essentials,New To Java...
# 15

> fastmike

>

> Again, spoonfeeding != teaching.

> Again, you are not helping here, you are in fact

> harming the learning process.

> Again, we are not impressed with your so-called

> coding abilities. Any one of us could just paste up

> the code and in fact create code far better than your

> ****, yet we know better.

> Again, you are doing the forum a great disservice.

>

> Please change your behavoir or just leave.

lol!

I thought that was kinda harsh... but, I guess you guys know him better than I do =)

Blue_Chimeraa at 2007-7-28 18:28:53 > top of Java-index,Java Essentials,New To Java...