• Post Reply Bookmark Topic Watch Topic
  • New Topic

Defining a class in a source code file  RSS feed

 
james madison
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am reading the following book: OCA Java SE7 Programmmer 1 Certificagtion Guide.

In that book on chapter 1 page 24, it says that "If you define a public class or an interface in your class, then it should match the name of the java source code file."
Does the statement above mean the following should compile fine ?

public class Animal{

public class Animal{ }
}


The author gives an example saying that if you have a class Multiple.java and in that class, you have:

public class Multiple{}, then it should compile.


But this does not compile. I tried it. Any ideas ?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This compiles just fine for me. What error messages are you getting?
 
james madison
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not what I meant. I meant something like:

public class Foo{

public class Foo { }
}

A class within a class. The author is saying this will compile. Please read the sentence in the quotation. I just wanted to know if the example I have is what the author meant or did he mean something else?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the quoted sentence is poorly worded. I think the author just means that if you have a public class:



...then your file should be called Foo.java.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james madison wrote:But this does not compile. I tried it. Any ideas ?

It is of TREMENDOUS help if you post the error message you got. Those messages aren't just booleans - it compiled/it didn't compile. When the compilation fails, it tells you WHY it failed. In this case, it says:

C:\slop>javac Foo.java
Foo.java:3: error: Foo is already defined in unnamed package
public class Foo { }
^
1 error

it is telling you that you have a class named Foo, then you try to create a NEW class named Foo. You can't do that. This error has nothing to do with the public/private/filenames. This will compile:

 
james madison
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, my bad. So, before I even compile, eclipse has thos red circles that appear if you screw up something. And when I hover over that, it is telling me "The nested type Foo cannot hide an enlosing type"
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that error message make sense to you? The "enclosing type" is Foo and you are hiding it because you defined another Foo inside. In other words, Ya can't do that!
 
james madison
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I know, that doesnt make sens to me either.
But, please look at the attachement.

Here's the answer to the question:

Answer: c, d
Explanation: Options (a) and (b) are incorrect.
Option (c) is correct because a Java source code file can define multiple interfaces
and classes.
Option (d) is correct because a public interface or class can be defined in a
Java source code file with a matching name. The public interface Printable can’t
be defined in the Java source code file, Multiple.java. It should be defined in
Printable.java.


qn1.PNG
[Thumbnail for qn1.PNG]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james madison wrote:
I know, that doesnt make sens to me either.
But, please look at the attachement.

What has this question got to do with your original question ?

james madison wrote:public class Foo{

public class Foo { }
}

A class within a class. The author is saying this will compile.

He's saying nothing of the kind.
Your code won't compile because you have a class called Foo inside another class called Foo.
In the author's example there are two separate classes and they have different names.
The author's example won't compile because he has a public class/interface in a file with a different name to the class/interface.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
d) is correct, because



...must be in a file named Printable.java.

Was that your question?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!