Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Simple Java Problems - 1-0

Simple Java Problems - 1-0
Thread Tools
Junior Member
Join Date: May 2002
Location: Australia
Status: Offline
Reply With Quote
Jul 27, 2002, 11:15 PM
 
I am learning Java from Thinking in Java version 2, I believe I have made a simple mistake, in writing out this exercise, can you help?

The code compiles without error it however returns the following error when you try to run it "Exception in thread "main" java.lang.NoClassDefFoundError: ATypeName". What am I doing wrong?

## ##

// 1-2:ATypeName.java

import java.util.*;

/** This is ATypeName Java Program
* ATypeName displays >>>
* @author Helmut Kurt Burri
* @author e-mail: /////////////
* @verion 1.0
* @since 28/07/02
*/

public class ATypeName {
/** this class stores data types */
int i;
float f;
boolean b;
}

class PrintClass {
/** this method initialises the data types found in class ATypeName */
public static void main(String[] args) {

// create a new instance of the class ATypeName
ATypeName a = new ATypeName();

a.i = 47;
a.f = 1.1f;
a.b = false;

System.out.print( "The integer is equal to" + a.i );
System.out.print( "The float is equal to " + a.f );
System.out.print( "The boolean is equal to " + a.b );
}
}

## ##
     
dogwood  (op)
Junior Member
Join Date: May 2002
Location: Australia
Status: Offline
Reply With Quote
Jul 28, 2002, 12:54 AM
 
When I compile the above code I get two Java class files one for each class. Is that normal. I am JJEdit, to compile my stuff.
     
Forum Regular
Join Date: Nov 2001
Location: Australia
Status: Offline
Reply With Quote
Jul 28, 2002, 12:59 AM
 
Try putting your ATypeName class in a seperate file called ATypeName.java, and rename the first one to PrintClass.java, then add 'import ATypeName;' to PrintClass.java. To compile you only need to type 'javac PrintClass.java' in the terminal.

good luck

<small>[ 07-28-2002, 02:02 AM: Message edited by: V0ID ]</small>
     
dogwood  (op)
Junior Member
Join Date: May 2002
Location: Australia
Status: Offline
Reply With Quote
Jul 28, 2002, 01:47 AM
 
Thanks it worked, But why is the question did it work? What difference does making it an include make to the compile stream?

Thanks again
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Jul 28, 2002, 05:50 AM
 
Java requires you to have a seperate .class file for every class; hence, it is a very good idea to have seperate .java files for them. When compiling, PrintClass knew about ATypeName because they were in the same file, but when you tried to run it, PrintClass had never heard of an ATypeName. The import statement tells one class or package about another class or package.
[vash:~] banana% killall killall
Terminated
     
Fresh-Faced Recruit
Join Date: Sep 2000
Location: Eugene, OR 97477
Status: Offline
Reply With Quote
Jul 31, 2002, 03:40 PM
 
All you have to do is drop the public modifier for the ATypeName class and save the file under PrintClass.java which has the main method.

-clay-
     
Fresh-Faced Recruit
Join Date: Jan 2002
Location: Cambridge, England
Status: Offline
Reply With Quote
Jul 31, 2002, 04:16 PM
 
Where is the encapsulation for i f and b?
mmmm - I'm a big Cinnamon bun.
     
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Jul 31, 2002, 04:56 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Gul Banana:
<strong>Java requires you to have a seperate .class file for every class; hence, it is a very good idea to have seperate .java files for them. When compiling, PrintClass knew about ATypeName because they were in the same file, but when you tried to run it, PrintClass had never heard of an ATypeName. The import statement tells one class or package about another class or package.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Yes, there are separate class files for every class.

But, there are NOT distinct .java files in ALL cases.

Although this is NOT what the original poster is doing, there is this concept of inner classes.

Inner classes are defined inside another class (ie: inside the containing class's .java file) and are appropriately scope limited, and do not require being imported by the containing class. In a nutshell, inner classes give another layer of indirection to the naming scheme.

The compiler will make class files for inner classes often with the name:

containingClass$innerClass.class

So, when you compile containingClass.java the compiler will make two files:

containingClass.class
and
containingClass$innerClass.class

This isn't really a reply to the original post, but more a clarification on class files in general.
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Aug 1, 2002, 06:36 PM
 
Actually he is making an inner class, that's why it initially compiled, and that's why there where two .class files created, as Kristoff explained.

The problem was that the outer class (if there was such a term) doesn't have a main method, and therefore running the class as so:

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">java ATypeName</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">fails.

I'm not sure WHY he's using inner classes, or if he realizes that he's doing that. To fix his problem, he could have made the outer class the inner class and vice-a-versa (which makes the most sense IMO), or the coder (I know not sex) could have added a main method to the outer class and had it call the main method in the inner class.

Matt Fahrenbacher
     
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Aug 3, 2002, 02:23 AM
 
You're right, but...

what I meant was:

That's not what the original post is trying to do.

One ought not to make inner classes unless they know what they are doing.

The intent of the original poster was not that of making an inner class. I am certain of this for obvious reasons.
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Jan 22, 2003, 12:42 PM
 
Originally posted by Ghoser777:
Actually he is making an inner class, that's why it initially compiled, and that's why there where two .class files created, as Kristoff explained.

The problem was that the outer class (if there was such a term) doesn't have a main method, and therefore running the class as so:
This is about as inaccurate a description if inner classes as I've ever heard.

Two classes in the same .java file are not necessarily nested (or inner) classes! The above classes are declared within the same source file - that is their only relation. A nested class is defined within another class, whereas these classes are peers - defined at the same level (though one is public and one is not). If a class does not reside between the open and close brace of another class - it is not a nested class.

It probably didn't work because the class PrintClass has package scope and there was a bug in the java executable (or the obvious, that he didn't have the ATypeName.class classfile in his classpath).
     
Fresh-Faced Recruit
Join Date: Nov 2002
Status: Offline
Reply With Quote
Jan 23, 2003, 02:13 AM
 
Hi all,

Just thought I'd throw in my opinion. I am also learning Java from a book, and I have also downloaded and had a brief look at "Thinking in Java".

I have no doubt the it is far more comprehensive than the book I am using, and would probably be a great secondary reference to clarify things that may not be been explained well enough in another text.

However, I found that the code samples were far too short. So short as to almost be meaningless. They are great for explaining what was covered immediately before them in the text, but they provide no real context.

Also, the fact that the examples, as seen here, place multiple classes in one file is a little confusing for the beginning programmer.

At this stage, I do not know enough to say: "Placing many classes in one file is a bad thing". But the book I have has been placing one class per file, with the exception of inner classes. To me this seems much clearer, and better practice.

Maybe someone with more experience could comment on this?

I know that "Thinking in Java" usually wins a lot of praise in reviews. However, I have an inkling that most of the reviewers are already familar with Java, or other object oriented languages.

Just for completeness, and this is in no way a recommendation, nor am I saying that it is better or worse than "Thinking in Java", the book I am using is "Java Software Solutions: Foundations of Program Design"

Bye
Gelfling
"The giant Grof was hit in one eye by a stone, and that eye turned inward so that it looked into his mind, and he died of what he saw there" -- The Forgotten Beasts of Eld, Patricia A. McKillip
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Jan 23, 2003, 04:13 AM
 
One interesting point about Thinking in Java is that it was written by a C++ guru.. Bruce Eckel is a very good technical writer, but it is also possible that the book is not all that beginner-oriented. One good beginner's Java textbook is "Object Oriented Programming With Java: An Introduction" by David Barnes.

Ghoser: absmiths is correct, the original poster's code does not create an inner class.

Kristoff: thanks for clarifying, I was just trying to simplify matters originally. Personally, I think inner classes (especially with the static/nested distinction) should not be in the language.. although useful, they're an overcomplex feature that seems far more C++ish to me.
[vash:~] banana% killall killall
Terminated
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jan 23, 2003, 06:52 PM
 
Just to be clear, here is the "compileable" version of your printclass with an inner class. For the record, I don't ever use inner classes.

Code:
// 1-2:PrintClass.java import java.util.*; /** This is PrintClass Java Program * PrintClass displays &gt;&gt;&gt; * @author Helmut Kurt Burri * @author e-mail: ///////////// * @verion 1.0 * @since 28/07/02 */ class ATypeName { /** this class stores data types */ int i; float f; boolean b; } public class PrintClass { /** this method initialises the data types found in class ATypeName */ public static void main(String[] args) { // create a new instance of the class ATypeName ATypeName a = new ATypeName(); a.i = 47; a.f = 1.1f; a.b = false; System.out.print( "The integer is equal to" + a.i ); System.out.print( "The float is equal to " + a.f ); System.out.print( "The boolean is equal to " + a.b ); } }
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Jan 23, 2003, 07:30 PM
 
Just to be clear, here is the "compileable" version of your printclass with an inner class.
As someone stated earlier, ATypeName is NOT an inner class. Try adding a private field to PrintClass and accessing it from ATypeName if you don't believe me.
For the record, I don't ever use inner classes.
Obviously not
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Jan 23, 2003, 09:42 PM
 
See, this is what I mean. Inner classes = confusion
For the record, for once and for all, this is what the file would look like if it was using an inner class:

Code:
// 1-2:PrintClass.java import java.util.*; /** This is PrintClass Java Program * PrintClass displays &gt;&gt;&gt; * @author Helmut Kurt Burri * @author e-mail: ///////////// * @verion 1.0 * @since 28/07/02 */ public class PrintClass { /** this method initialises the data types found in class ATypeName */ public static void main(String[] args) { // create a new instance of the class ATypeName ATypeName a = new ATypeName(); a.i = 47; a.f = 1.1f; a.b = false; System.out.print( "The integer is equal to" + a.i ); System.out.print( "The float is equal to " + a.f ); System.out.print( "The boolean is equal to " + a.b ); } private class ATypeName { /** this class stores data types */ public int i; public float f; public boolean b; } }
This would compile to two files: PrintClass.class and PrintClass$ATypeName.class
[vash:~] banana% killall killall
Terminated
     
Junior Member
Join Date: Sep 2000
Status: Offline
Reply With Quote
Jan 24, 2003, 04:58 AM
 
Originally posted by Gelfling:
Hi all,

Just thought I'd throw in my opinion. I am also learning Java from a book, and I have also downloaded and had a brief look at "Thinking in Java".
I'm not a big fan of this book - its all C++'y and hacky, but some people love it...

[/B]
Also, the fact that the examples, as seen here, place multiple classes in one file is a little confusing for the beginning programmer.
...
Maybe someone with more experience could comment on this?
[/B]
My advice would be would be to have a seperate file for each class, and to avoid inner classes.

I found 'learn java in 21 days' pretty good, back in the Java 1.02 days. I'd also recommend a seperate book on OO
Nobody made a greater mistake than
he who did nothing because he could only
do a little. Edmund Burke
     
Forum Regular
Join Date: May 2001
Location: Hong Kong
Status: Offline
Reply With Quote
Jan 26, 2003, 02:41 PM
 
I prefer separate into two java files because of re-usability.

Also, ATypeName does not have constructor and i, f, b should declare as private. To allow other classes to access the attributes of ATypeName, add some "set" and "get" methods to ATypeName.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:15 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2