• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Defining a class in a source code file

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d) is correct, because



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

Was that your question?
 
Nothing up my sleeve ... and ... presto! A tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic