Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static problem  RSS feed

 
Shane Thornton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made this as an example..

<example>
public class Animal
{
String name;
int size;
String roar;

public static void main(String[] args){
Wolf w = new Wolf();
w.name = "Wolfman";
w.size = 3;
w.roar = "Woooooooo!";
System.out.println(w.roaring());
}

public String roaring(){
return "I am "+name+" and I "+roar+" -- plus my size is "+size;
}

private class Wolf extends Animal
{
};
};
</end example>

Not sure why this doesn't run. How can I fix it. (I know I should give Wolf its own file but from what I understand you only have to do that with public classes and I declared this as private.

As a bonus question is there any info I could have on methods of putting multiple classes in one .java file (or can I only use privates? Sucks having so many .java & .class files.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to make Wolf a static class. Right now it's an inner class which means it's going to require an enclosing instance of Animal which isn't what you intended I'm sure.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shane Thornton:
...is there any info I could have on methods of putting multiple classes in one .java file (or can I only use privates? Sucks having so many .java & .class files.

You can have at most one top-level public class or interface definition in a .java file. However, if your top-level classes have default access (instead of public), then you can put as many as you like into a single .java file.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you do lump all the code into one *.java file, the compiler will still generate a *.class file for each of your inner classes, regardless of the access modifyer. Out of curiosity, what do you find distressing about having multiple *.java and *.class files?
[ June 02, 2006: Message edited by: Garrett Rowe ]
 
Shane Thornton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just tiddier I suppose, also, then I don't have to open a bunch of files to work on the same project.

edit, found this solution thanks to your responses

public class Animal{

String name;
int size;
String roar;

public static void main(String[] args){
Wolf w = new Wolf();
w.name = "Wolfman";
w.size = 3;
w.roar = "Woooooooo!";
System.out.println(w.roaring());
}

public String roaring(){
return "I am "+name+" and I "+roar+" -- plus my size is "+size;
}

static class Wolf extends Animal{
};

};
[ June 04, 2006: Message edited by: Shane Thornton ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the semicolon after the end of each of your class definitions is allowed, but neither required nor encouraged; just leave it off.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

You can have at most one top-level public class or interface definition in a .java file. However, if your top-level classes have default access (instead of public), then you can put as many as you like into a single .java file.


While I've yet to personally encounter a compiler that allowed it that's not entirely true. That requirement is left up to the compiler and there is at least one compiler out there that doesn't enforce it.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only reason you declare the wolf class static is so you can do this

right?



is that correct?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!