-18

I got a question on an exam.

Does every .class (not .java) file contain a public class?

Yes? No?

Boann
  • 48,794
  • 16
  • 117
  • 146
Rdomi
  • 45
  • 3
  • 6
    are you doing a test exam right now? – Valentin Ruano Jan 31 '19 at 20:33
  • https://stackoverflow.com/q/5204385/1531971 https://stackoverflow.com/q/16971610/1531971 https://www.coursehero.com/file/p1e2uc8/Format-of-a-Java-Program-Every-Java-file-contains-a-public-class-interface-or/ –  Jan 31 '19 at 20:36
  • 1
    @ValentinRuano No, Im just curious, I chose "no". – Rdomi Jan 31 '19 at 20:42
  • 2
    This question is not mean for SO ... we expect that you actually try to answer it yourself and if you get stuck then we may come in and help... usually you would have some code, command line or something that you include in your question.... you question does not show that you even try.... more like drop/dump it and see what happens. Google it first, try it yourself... eg. what if you try to compile a non-public class containing .java doesn't the compiler creates a .class file? is a simple like that to find the answer, right? – Valentin Ruano Jan 31 '19 at 21:15
  • This is the reason why question like yours a suspect of someone that have a homework and they do want to bother to do (or don't have the time) or is in a exam room looking for a quick fix... not that that is necessarily your case but it simply looks like it ... and often is in fact the case. This is more obvious with homework though. – Valentin Ruano Jan 31 '19 at 21:17

4 Answers4

2

No, can have some classes as private, protected, default and public. From these four type

1

No, class files are generated (also) for less visible and even "inner" classes.

xerx593
  • 12,237
  • 5
  • 33
  • 64
1

Nope, there are 4 different types:

  • Public
  • Private
  • Protected
  • Default
0

No access modifiers include public, protected, and private.

Example:

public class foo {}

private class foo {}

protected class foo {}

You can also have a class without an access modifier.

class foo {}

However, if you want to be able to fully access your class from another class use the public access modifier.

See here for more information : Examining Class Modifiers and Types