File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ATTENTION. Study few of the given rules. Will help in passing exam. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ATTENTION. Study few of the given rules. Will help in passing exam." Watch "ATTENTION. Study few of the given rules. Will help in passing exam." New topic
Author

ATTENTION. Study few of the given rules. Will help in passing exam.

kishor kotecha
Greenhorn

Joined: Oct 02, 2000
Posts: 27
Hello Everyone. In the process of my studying for SCJP, I compiled these FEW rules from JLS (Java rules bible). Published here for the benefit of all SCJP candidates. Print and Study them. Will be useful definately. If you have compiled such a list from authenticate source , please let me know. It makes learning easy. I have ommitted few simple rules from here like the expression of if statement must be boolean type.
�A compile-time error occurs if a class has the same simple name as any of its enclosing classes or interfaces.
�A compile-time error occurs if the same modifier appears more than once in a class declaration.
�If a class that is not abstract contains an abstract method, then a compile-time error occurs.
�A compile-time error occurs if an attempt is made to create an instance of an abstract class using a class instance creation expression
�A compile-time error occurs if the name of a final class appears in the extends clause of another class declaration; this implies that a final class cannot have any subclasses.
�A compile-time error occurs if a class is declared both final and abstract, because the implementation of such a class could never be completed.
�Because a final class never has any subclasses, the methods of a final class are never overridden
�It is a compile-time error if a class depends on itself.
�A compile-time error occurs if the same interface is mentioned two or more times in a single implements clause.
�It is a compile-time error for the body of a class declaration to declare two fields with the same name.
�A compile-time error occurs if the same modifier appears more than once in a field declaration, or if a field declaration has more than one of the access modifiers public, protected, and private.
�It is a compile-time error if a blank final class variable is not definitely assigned by a static initializer of the class in which it is declared.
�A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.
�A compile-time error occurs if a final variable is also declared volatile.
�It is a compile-time error for the body of a class to have as members two methods with the same signature.
�If two formal parameters of the same method or constructor are declared to have the same name (that is, their declarations mention the same Identifier), then a compile-time error occurs.
�A compile-time error occurs if the same modifier appears more than once in a method declaration, or if a method declaration has more than one of the access modifiers public, protected, and private.
�A compile-time error occurs if a method declaration that contains the keyword abstract also contains any one of the keywords private, static, final, native, strictfp, or synchronized.
�A compile-time error occurs if a method declaration that contains the keyword native also contains strictfp.
�It is a compile-time error for a private method to be declared abstract.
�It is a compile-time error for a static method to be declared abstract.
�It is a compile-time error for a final method to be declared abstract.
�It is a compile-time error to attempt to override or hide a final method.
�It is a compile-time error for a final method to be declared abstract.
�A compile-time error occurs if a native method is declared abstract.
�A compile-time error occurs if any ClassType mentioned in a throws clause is not the class Throwable or a subclass of Throwable. It is permitted but not required to mention other (unchecked) exceptions in a throws clause.
�A compile-time error occurs if a method declaration is either abstract or native and has a block for its body.
�A compile-time error occurs if a method declaration is neither abstract nor native and has a semicolon for its body.
�If a method is declared void, then its body must not contain any return statement that has an Expression.
�A compile-time error occurs if an instance method attempts to override a static method.
�A compile-time error occurs if a static method hides an instance method.
�If a method declaration overrides or hides the declaration of another method, then a compile-time error occurs if they have different return types or if one has a return type and the other is void. Moreover, a method declaration must not have a throws clause that conflicts with that of any method that it overrides or hides; otherwise, a compile-time error occurs.
�If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.
�If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error occurs.
�If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.
�If a return statement appears anywhere within a static initializer, then a compile-time error occurs.
�If the keyword this or the keyword super appears anywhere within a static initializer, then a compile-time error occurs.
�A class may not declare two constructors with the same signature, or a compile-time error occurs.
�It is a compile-time error for a constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this.
�A compile-time error occurs if a default constructor is provided by the compiler but the superclass does not have an accessible constructor that takes no arguments.
�a method declared in an interface must not be declared final or a compile-time error occurs. However, a method declared in an interface may be implemented by a method that is declared final in a class that implements the interface.
�A component of an array is accessed by an array access expression that consists of an expression whose value is an array reference followed by an indexing expression enclosed by [ and ], as in A[i]. All arrays are 0-origin. An array with length n can be indexed by the integers 0 to n-1.
�Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion and become int values. An attempt to access an array component with a long index value results in a compile-time error.
�The type of the Switch Expression must be char, byte, short, or int, or a compile-time error occurs.
�Every case constant expression associated with a switch statement must be assignable to the type of the switch Expression.
�No two of the case constant expressions associated with a switch statement may have the same value.
�Only one default label may be associated with the same switch statement.
�If a break statement is not enclosed by a switch, while, do, or for statement , a compile-time error occurs.
�If no labeled statement with Identifier as its label encloses the break statement, a compile-time error occurs.
�If a continue statement is not enclosed by a while, do, or for statement , a compile-time error occurs.
�If no labeled statement with Identifier as its label encloses the continue statement, a compile-time error occurs.
�A continue statement with label Identifier attempts to transfer control to the enclosing labeled statement that has the same Identifier as its label; that statement, which is called the continue target, then immediately ends the current iteration and begins a new one. The continue target must be a while, do, or for statement or a compile-time error occurs.
�If a label of continue statement is not assigned to while, do, or for statement, a compile-time error occurs.
�A compile-time error occurs if a return statement appears within an instance initializer or a static initializer.
�A return statement with an Expression must be contained in a method declaration that is declared to return a value (�8.4) or a compile-time error occurs. The Expression must denote a variable or value of some type T, or a compile-time error occurs. The type T must be assignable (�5.2) to the declared result type of the method, or a compile-time error occurs.
�It is a compile-time error if a statement cannot be executed because it is unreachable.
�The keyword this may be used only in the body of an instance method, instance initializer or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.
[This message has been edited by kishor kotecha (edited October 09, 2000).]
[This message has been edited by kishor kotecha (edited October 09, 2000).]
[This message has been edited by kishor kotecha (edited October 09, 2000).]
[This message has been edited by kishor kotecha (edited October 10, 2000).]
Harps S
Greenhorn

Joined: Oct 09, 2000
Posts: 9
Hey Kishore,
Great job thanx you made life simpler
Harpal
------------------
pete hesse
Ranch Hand

Joined: Aug 29, 2000
Posts: 44
"A compile-time error occurs if attempt is made to override a static method."
Sorry to have to rain on your parade, but this is incorrect. You can override static methods, the one restriction is you cannot override them to be non-static.
Your list is a good resource.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Nice List. Pete is right though, you can override a static method as long as you make it static also. I think what you meant is you can't override a final method. But thanks for the info, I cut and pasted it to a word doc for reference.
Bing Lu
Ranch Hand

Joined: Oct 10, 2000
Posts: 42
think you got the break continue wrong. Isn't it continue can
be only used in loops and break can be used with labels w/o loops?
Sandeep Lodhia
Ranch Hand

Joined: Oct 09, 2000
Posts: 105
Thanks
VINAYAK KAURWAR
Greenhorn

Joined: Oct 03, 2000
Posts: 3
Hi, Kishor I found your messege cum article very useful. I was not very clear on compile time errors and that's where I was making mistakes. I planning to take exam sometime next week...so it was a great help. Thanks to you and Javaranch.
Regards,
Vinayak
------------------
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Bing, not sure if I understand you question, but a continue can only be used with while, do, and for loops, and if you use it with a label such as:
continue mainLoop;
Then you must have a mainLoop lable set up somewhere.
A break can be used for while, do and for loops plus it is used for switch statements. With loops, the same rule for labels applies as it does with continue. So I think kishor has it right.
Bing Lu
Ranch Hand

Joined: Oct 10, 2000
Posts: 42
what i'm trying to say is that continue Label will only work if
the statements following the Label is for loop/ while/ do-while.
pls correct me if I'm wrong
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104

Thats a great Job Kishore.
pete hesse is also correct that static methods can not be overidden to be non-static rather we can overide them but they have to be static only.
Manish
[This message has been edited by Manish Singhal (edited October 10, 2000).]
Sudhir Bangera
Ranch Hand

Joined: Oct 10, 2000
Posts: 50

Thanks Kishor. I just registered myself few minutes back and already found something very useful here. I like it already.
Sudhir
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, I tend to think that a static method can not be overriden. But that it can be "hidden" by a static method of the same signature in a child class.
Any comment?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Ricky, that is what overriding is. Creating a method in a subclass with the same signature (number and types of arguments) as the method in the superclass.
kishor kotecha
Greenhorn

Joined: Oct 02, 2000
Posts: 27
Thanks Pete. The correction has been made regarding the "static overriding" error.
"A compile-time error occurs if an instance method attempts to override a static method."
Thanks everybody. It was really nice to see such a good response.

[This message has been edited by kishor kotecha (edited October 10, 2000).]
umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
Hi Kishor,
Thanks yaar, that was really very nice of u for providing with this list. U had really helped a lot.
Lee Reedy
Greenhorn

Joined: Sep 08, 2000
Posts: 25
Perhaps the moderators could add this to the new FAQ?!?!
Great job, thanks for posting =)
Surya Bahadur
Ranch Hand

Joined: Sep 28, 2000
Posts: 88
Maybe this link will also be helpful to all SCJP aspirants
http://www.javaranch.com/ubb/Forum24/HTML/001524.html
Surya B
Ray Hsieh
Ranch Hand

Joined: Sep 20, 2000
Posts: 59
Hi Bill,
I disagree, I think Ricky is right. Overriding and hiding are *different*. Check this out:
class base {
public static void blah() {
System.out.println("base blah");
}
public void bleh() {
System.out.println("base bleh");
}
}
public class testover extends base {
public static void blah() {
System.out.println("derived blah");
}
public void bleh() {
System.out.println("derived bleh");
}
public static void main(String[] args) {
base b = new testover();
b.blah();
b.bleh();
testover c = new testover();
c.blah();
c.bleh();
}
}
bleh is overridden in class testover, it's just like a virtual method in C++. At run time, which version gets called depends on the actual instance. But hiding is different. Which version gets called depends on the type of the reference.
Cheers
Originally posted by bill bozeman:
Ricky, that is what overriding is. Creating a method in a subclass with the same signature (number and types of arguments) as the method in the superclass.

bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Thanks Ray, something I learned today. From your code it looks like that if you have a static method in the base class, and override it with a static method in the subclass, then in your first instance (base b = new testover() you will get the call to the base class, but in your second instance (testover c = new testover() you will get the call to the subclass. This however is only tru with static methods, correct?
kishor kotecha
Greenhorn

Joined: Oct 02, 2000
Posts: 27
I will appreciate and it will be helpful to all if i get some more concise compiled lists for scjp
Ray Hsieh
Ranch Hand

Joined: Sep 20, 2000
Posts: 59
Hi Bill,
Well, by default, non-static methods in Java are virtual... so, yeah, that will happen if the method is static. If it's not static, and the instance is actually that of a derived class, that class's version will be called.
Cheers!
[This message has been edited by Ray Hsieh (edited October 11, 2000).]
roopesh patil
Greenhorn

Joined: Oct 14, 2000
Posts: 3
Hi Kishor. Great job. Very useful to study these rules. I copied them and printed for my own handy reference. PLEASE post more such lists.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ATTENTION. Study few of the given rules. Will help in passing exam.