Stack Class


Greetings from a newbie.


As a noob I'm reading Herbert Schildt's Java-The Complete Reference-Eleventh Edition (2019).  

Mods, I apologize upfront if this thread is not nested in the correct forum.  


Please forgive my ignorance with the use of terms...



I have two simple questions.

I'm taking each code sample in the book and writing it to IntelliJ (IDEA 2019.3.2) as a form of learning.  All was straight forward until chapter 6 (Introducing Classes).  When describing a simple class and providing an example it is discovered that the code stops using the following:

public class Main {

public static void main(String args [ ] ) {...


In chapter 6 all the code looks more like the following:

package BoxDemo.java2;
/* A program that uses the box class (pg 119).

Call this file


class Box {
double width;
double height;
double depth;

// this class declares an object of type Box.
class BoxDemo {
public static void main(String[] args) {
Box mybox = new Box();
double vol;

// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;

// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;

System.out.println("Volume is " + vol);


At first, I kept receiving errors when compiling; however, after a bit of trial and error, I discovered that BoxDemo needed to be manually assigned as main.


Pg 120 refers to the need to " the file that contains this program, because the main() method is in the class called BoxDemo, not the class called Box." I played around a bit to find that I needed to manually assign as main; however, Is there a template I can start with that will automatically assign main? Or do I just need to know this principle?  


Chapter 6 later introduces a Stack Class but I can not get any of the examples to work on IntelliJ.  Here is the first example:

//This class defines an integer stack that can hold 10 values

class Stack //IntelliJ is not allowing me to make this a main
int stck[] = new int [10];
int tos;

// Initialize top-of-stack
Stack() {
tos = -1;

// Push an item onto the stack
void push(int item) {
if (tos==9)
System.out.println("Stack is full.");
stck[++tos] = item;
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
return stck[tos--];

I get the following error message:

Error: Could not find or load main class
Caused by: java.lang.ClassNotFoundException:


What do I need to do to get the following code to compile without errors?



I appreciate any insights the team is willing to offer.








That has been my understanding as well; thank you for confirming.  I'll also take some time to read through your suggested links (Thank you).


If I may pick your brain a bit more, I've compiled the following code (copied directly from the textbook) to understand the push() and pop() method:


import java.util.Stack;

class TestStack {
public static class main(String args []) {
Stack mystack1 = new Stack ();
Stack mystack2 = new Stack ();

// push some numbers onto the stack
for (int i=0; i<10; i++) mystack1.push (i);
for (int i=10; i<20; i++) mystack2.push (i);

// pop those numbers off the stack
System.out.println("Stack in mystack1: ");
for (int i=0; i<10; i++)

System.out.println("Stack in mystack2: ");
for (int i=0; i<10; i++)



However, IntelliJ reveals the following errors:

Information:java: Errors occurred while compiling module 'TestStack'
Information:javac 13.0.1 was used to compile java sources
Information:1/25/20, 7:36 AM - Build completed with 25 errors and 0 warnings in 2 s 329 ms
Error:(6, 29) java: '{' expected
Error:(11, 9) java: illegal start of type
Error:(11, 25) java: illegal start of type
Error:(11, 30) java: <identifier> expected
Error:(11, 47) java: <identifier> expected
Error:(11, 50) java: <identifier> expected
Error:(12, 9) java: illegal start of type
Error:(12, 26) java: illegal start of type
Error:(12, 31) java: <identifier> expected
Error:(12, 48) java: <identifier> expected
Error:(12, 51) java: <identifier> expected
Error:(15, 27) java: <identifier> expected
Error:(15, 28) java: illegal start of type
Error:(16, 9) java: illegal start of type
Error:(16, 25) java: illegal start of type
Error:(16, 30) java: <identifier> expected
Error:(17, 31) java: <identifier> expected
Error:(17, 44) java: <identifier> expected
Error:(19, 27) java: <identifier> expected
Error:(19, 28) java: illegal start of type
Error:(20, 9) java: illegal start of type
Error:(20, 25) java: illegal start of type
Error:(20, 30) java: <identifier> expected
Error:(21, 31) java: <identifier> expected
Error:(21, 44) java: <identifier> expected


I've played around with some of the IntelliJ suggestions but I end up making it

What am I missing?






Should be "public static void main" instead of "public static class main".

You can type "main" and press Tab and IntelliJ IDEA will generate the method stub for you (the same works with "psvm").



Yup.  I forgot to return class back to main when I was playing around.  While the code now runs, I notice the following warning:

"Raw use of parameterized use of Stack". 

I'm beginning to think that the reference book is a bit dated for use with IntelliJ 3.2?  


Definitely a lot to learn.  


Thank you again Serge, for indulging my curiosity.





Please sign in to leave a comment.