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
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.");
> public class Stock
should be
static class Stock
hiwaa at 2007-7-28 18:28:48 >

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.
> > 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
Or even better.
public class Stock
{
//...
}
public class Lab4_1
{
public static void main(String[] args)
{
//...
}
}
> Or even better.
Captain Morgan is right, my bad: I answered the question; he fixed the underlying problem.
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
> 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
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!
Just try putting them in separate files and compile them.
> 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).
OKI
will try that! Thank you both for the help! I will split the remaining two stars between you :)
Take care!
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);
}
}
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.
> 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 =)