NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

New to Java: getting some error in Netbeans

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
anantvu2205



Joined: 22 May 2012
Posts: 3

PostPosted: Tue May 22, 2012 2:54 pm    Post subject: New to Java: getting some error in Netbeans Reply with quote

Hi,
I am a newbie, and have written this simple program in Netbeans:

package puppy;

class PuppyDemo {
public int weight;
public void setWeight(int newWeight) {
weight=newWeight;
}
public int getWeight() {
return weight;
}

}
public class Puppy {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
PuppyDemo pd=new PuppyDemo();
pd.setWeight(10);
System.out.println("Puppy's age: "+pd.getWeight());

}
}
Everything looks fine to me, but I am getting this error :
Exception in thread "main" java.lang.VerifyError: Constructor must call super() or this() before return in method puppy.PuppyDemo.<init>()V at offset 0
at puppy.Puppy.main(Puppy.java:23)

And also if I declare the class PuppyDemo as public, it says "class PuppyDemo is public, should be declared in a file named PuppyDemo.java"
Why am I having this problem?
Back to top
nylla



Joined: 22 May 2012
Posts: 6

PostPosted: Tue May 22, 2012 9:14 pm    Post subject: Reply with quote

I don't know enough to help you, but I would highly recommend the free downloadable book/course

thinkapjava

When learning a new language it's best to let a course guide you through the basics until you have a little understanding of how it works.
Back to top
Dave Bender
Posted via mailing list.





PostPosted: Tue May 22, 2012 9:27 pm    Post subject: New to Java: getting some error in Netbeans Reply with quote

I copied your code into a new class file, named Puppy, and it compiled and
ran fine. Looks fine to me, too. So I can't help you on the first issue
you raised.

On the second issue, adding 'public' in front of 'class PuppyDemo' you are
getting that error because each Java source code file can contain only one
public class. It can contain many classes, but only one that is public, or
visible to the outside world.

Since you've already defined 'public class Puppy' as the public class in
that source code file, the compiler is reminding you of that. If you want
two public classes, they each need to be in their own file.

Hope that helps.


Dave



-----Original Message-----
From: address-removed [mailto:address-removed] On
Behalf Of anantvu2205
Sent: Tuesday, May 22, 2012 9:55 AM
To: address-removed
Subject: [nbusers] New to Java: getting some error in Netbeans

Hi,
I am a newbie, and have written this simple program in Netbeans:

package puppy;

class PuppyDemo {
public int weight;
public void setWeight(int newWeight) {
weight=newWeight;
}
public int getWeight() {
return weight;
}

}
public class Puppy {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
PuppyDemo pd=new PuppyDemo();
pd.setWeight(10);
System.out.println("Puppy's age: "+pd.getWeight());

}
}
Everything looks fine to me, but I am getting this error :
Exception in thread "main" java.lang.VerifyError: Constructor must call
super() or this() before return in method puppy.PuppyDemo.<init>()V at
offset 0
at puppy.Puppy.main(Puppy.java:23)

And also if I declare the class PuppyDemo as public, it says "class
PuppyDemo is public, should be declared in a file named PuppyDemo.java"
Why am I having this problem?
Back to top
anantvu2205



Joined: 22 May 2012
Posts: 3

PostPosted: Wed May 23, 2012 1:32 am    Post subject: Thanks Reply with quote

Hi,
Actually I had opened another project that I had opened along with this program and that was giving some errors. So that problem is resolved. And yes, when i declared only one class as public, or if i copy the other class in another file, and declare it as public, its working fine. Thanks a lot. But why this restriction of declaring only one class in a package as public? What will happen if we are able to declare all classes as public?
Back to top
Gregg Wonderly
Posted via mailing list.





PostPosted: Wed May 23, 2012 1:32 pm    Post subject: New to Java: getting some error in Netbeans Reply with quote

On 5/22/2012 8:32 PM, anantvu2205 wrote:
Quote:
Hi,
Actually I had opened another project that I had opened along with this program
and that was giving some errors. So that problem is resolved. And yes, when i
declared only one class as public, or if i copy the other class in another file,
and declare it as public, its working fine. Thanks a lot. But why this restriction
of declaring only one class in a package as public? What will happen if we are able
to declare all classes as public?

Java utilizes runtime binding of classes to ".class" files using the name of the
Class. When there are multiple "public" classes in the same file, it represents
a disconnect, with the design of the language. The intent, is that you should
use separate files for each class, regardless of if they are public or not.

The fact that there can be non-public classes declared in the same file as a
public class, is an old "feature" provided in the beginning days of Java
applets, back in the 1990's. Web servers were not very efficient at downloading
multiple files, like they are now, and so the decision was made to let applets
try to all be in one .class file for downloading speedups and some convenience
of isolation of "listener" classes. Now that we have "inner classes" and
"anonymous inner classes", fast web servers, as well as most IDEs build
everything into a .jar file for you, everything can happen in "one" download in
most cases.

Just split your classes in to individual files, named with the same name as the
class (whether the classes are public, protected or private), put them in
directory trees with the same structure as the package name, and you will not
have to fight with the IDE about how it sees your source files.

Gregg Wonderly
Back to top
anantvu2205



Joined: 22 May 2012
Posts: 3

PostPosted: Wed May 23, 2012 2:36 pm    Post subject: Reply with quote

Yes, thats right Gregg. I had been having this confusion for a long time over why only one class in a file can be made public, and now its cleared. Either I can put only one class in one file, or else I can put all the classes in a single file with only one class declared as public.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo