• 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

SCPJ 1.4 - Dan's questons.. not clear

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.danchisholm.net/july21/topic/section1/class1.html


Question 1

public class Basics {} // 1
class Basics1 {} // 2
protected class Basics2 {} // 3
private class Basics3 {} // 4
Class Basics4 {} // 5

Suppose these are top-level class declarations and not nested class declarations; and suppose that all of the declarations are contained in one file named Basics.java. Compile-time errors are generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. 5


The answer given is:

1c d e 3 4 5 If a class C is declared as a member of an enclosing class then C may be declared using no access modifier or any of the three access modifiers, private, protected or public. However, if class C is not a local class, anonymous class or a member of an enclosing class or interface; then C may be declared with the public modifier or with package access (i.e. no modifier). The other two access modifiers, private and protected, are not applicable to any class that is not a member class. The class declaration, Class Basics4 {}, generates a compile-time error, because all of the letters of the reserved word class must be lower case.


<<<<<< My verification>>>>>>>>>>>>>

when I have written the above class and compiled it compiled with out errors. Confused with the funda explained.

<<<<<<<<<<<<<<< ends here >>>>>>>>>>>>>>>>>>

Appreciate clarification.

Thanks
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when I have written the above class and compiled it compiled with out errors.



compiles without errors ??
[ October 21, 2005: Message edited by: Seb Mathe ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this thread...

https://coderanch.com/t/251322/java-programmer-SCJP/certification/Nested-Class
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seb

Yes. compiles with out errors. I am using j2sdk1.4.2_09

You try and pl give me your views too.

Appreciate some feed back.

Thanks
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
scjpexam2000@yahoo.com

The above e-mail for Dan is bouncing back

Appreciate if any one knows Dan's correct e-mail

Thanks
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sab

Just a small change, I removed last one that starts with 'Class' instead of 'class' ( case issue one.. as I know that)

Thanks and regret for any confusion from my side
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc

As per the info and link given by you, it states that the nested classes can use private and protected access modifiers.

Surprisingly that is also a question from Dan's site answered by you.

Even Dan's answer also says the same thing.

<<<<<<<<<<<<<<<< MyNested.java>>>>>>>>>>>>>>>>>>>>


public class MyNested{
class Basics1{};
public class Basics2{};
protected class Basics3{};
private class Basics4{};

}


<<<<<<<<<<<<<<<<< ends here>>>>>>>>>>>>>>>>>>>>

I hope I made the file as described in the question.

Appreciate clarification.

Thanks
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I have made as a non nested class it had thrown the errors


<<<<<<<<<<< code starts here >>>>>>>>>>>>>>>>>

public class Base{
Basics1 b1= new Basics1();
Basics2 b2= new Basics2();
Basics3 b3= new Basics3();
}

class Basics1{}

protected class Basics2{}

private class Basics3{}


<<<<<<<<<<<<<<< code ends here >>>>>>>>>>>>>

Now when I compile, I get the errors thrown for Basics2 and Basics3
classes



<<<<<<<<<<<<<<<<<< error starts here >>>>>>>>>>>>>>>>>>


C:\temp>javac Base.java
c:\temp\Basics2.java:1: modifier protected not allowed here
protected class Basics2{}
^
c:\temp\Basics3.java:1: modifier private not allowed here
private class Basics3{}
^
2 errors

C:\temp>



<<<<<<<<<<<<<<<<< error ends here >>>>>>>>>>>>>>>>>>>>>>>>

His question may be asking this way. May be I udersttod wrongly.

Pl correct me if I am wrong.....

Thanks
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Puthriah Sarma:
... His question may be asking this way...


I believe that's the case. Doesn't he say, "Suppose these are top-level class declarations and not nested class declarations..."?

Top-level class declarations with private or protected modifiers will not compile.
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc....
 
reply
    Bookmark Topic Watch Topic
  • New Topic