Author | Topic: Traps to be aware of in any SCJP test !!!!! :) |
maha anna bartender |
posted May 17, 2000 11:02 AM
This idea born from Mr. Ajith. All can contribute to this ** LET US SHARE OUR KNOWLEDGE AND GROW WITH EVERYONE ** thread . So please do share any small points which you think we should be aware of. Thank you.
|
maha anna bartender |
posted May 17, 2000 11:05 AM
I am posting this on behalf of Ajith. Ajith's contribution
|
bongadi greenhorn |
posted May 17, 2000 02:46 PM
Here's my contribution - Classes derived from abstract and not providing body for all abstract methods.
quote:
|
Jim Yingst sheriff |
posted May 17, 2000 07:01 PM
have a list of such gotchas: Things to look out for [This message has been edited by Jim Yingst (edited May 17, 2000).]
|
satya5 ranch hand |
posted May 17, 2000 10:50 PM
These are some which I wrote so I SHOULD check before entering the exam hall:
If any of this is wrong, I am assuming the moderators will Thanks. Suresh: - satya [This message has been edited by satya5 (edited May 17, 2000).] [This message has been edited by satya5 (edited May 18, 2000).]
|
Suresh ranch hand |
posted May 18, 2000 01:21 AM
satya, one small correction in your list.
quote: The above statement will give a compiler error stating: quote: And, that's a great list Maha, Ajith, satya and Jim. Will be very useful for people like me, who make careless mistakes!! Regards, [This message has been edited by Suresh (edited May 18, 2000).] [This message has been edited by Suresh (edited May 18, 2000).]
|
jayashree murugan greenhorn |
posted May 18, 2000 05:22 AM
Maha Anna I have a doubt. What does "Math class being an option for immutable classes !!" mean?
|
Ajith Kallambella bartender |
posted May 18, 2000 05:40 AM
Jayashree, Consider this question.
Because Math class cannot be instantiated, there is no question of immutability right? That's what I meant. Sorry for my brevity. Ajith
|
Ajith Kallambella bartender |
posted May 18, 2000 06:49 AM
Here goes some more.
More coming..... [This message has been edited by Ajith Kallambella (edited May 24, 2000).]
|
Sushma ranch hand |
posted May 18, 2000 09:22 AM
Hi , RandomAccessFile does inherit from Object and also it does implement DataOutput and dataOuput interfaces,is this what Ajit meant to say??? I 'm sorry if i'm wrong. Also, can u throw some light on 'Arrays are final objects'.... Regards, Sushma
|
Ajith Kallambella bartender |
posted May 18, 2000 09:53 AM
Sushma, Thanks for noting the error about RandomAccess files. I fixed it. I was talking about java.util.Arrays. After seeing your note I realised it is likely to cause confusion, so I have removed it. I am not even sure if it is an objective for the JCJP. However, FYI the java.util.Arrays cannot be instantiated, and cannot be extended. Just like Math class, it is a helper class (a.k.a. Static factory ) with lot of utility methods to sort, search ,manipulate arrays as list etc. Thanks again,
|
Sushma ranch hand |
posted May 18, 2000 10:02 AM
I thought you were talking about normal arrays. Thanks a lot for your points. They are very helpful. Sushma
|
Ajith Kallambella bartender |
posted May 18, 2000 10:19 AM
Sushma, I was thinking about it, Array objects are indeed final. Once created, they cannot be changed. I am not talking about their contents, but the actual Array object, which "holds" the content. Think about it, if you create an array of 3 elements, you cannot change it to accomadate 4 elements. Can you?? Infact the length attribute of arrays( ie., the array type ) is declared as public and final which means it cannot be changed once created. It's little tricky, but a very valid point. If I had a true/false question asking whether arrays are final, I would vote true. I am making sense here? Perhaps I should put that back into my list. Comments?? Ajith [This message has been edited by Ajith Kallambella (edited May 18, 2000).]
|
Surya B ranch hand |
posted May 18, 2000 10:52 AM
Please correct me if any of the statements are ambiguous When the & and | operators are used in an expression both operands are evaluated. When the && and | | operators are used then left operand is checked first and if it is not needed does not evaluate the right operand to determine the result. In switch...case after default you can have a break or you can ignore it,either way its ok. Cases can be stacked up one on other to have multiple matches for a particular piece of code. public class VowelsAndConsonants {
When u call one constructor from other use 'this' keyword which should be the first statement in the constructor.You can call only one constructor. You can call a method to provide an initialization value for the variable at the class level and at the method level itself. Order of Initializationvariables will be initialized depending on the order that the variables are defined within the class.Even if the variable definitions are scattered thru out method definitions,the variables are initialized before any methods can be called....even the constructor. You can define multi-dimensional arrays in the following manner: int a[][]=new int[1][2];---Is OK.
int c[][]={{1,2,3,},{4,5,6},};--Don't use the new int keyword.The comma after the last value is optional.
When using final with primitives final makes the value a constant,but with an object handle a final makes the handle a constant.THE HANDLE MUST BE INITIALIZED TO AN OBJECT AT THE POINT OF DECLARATION AND THE HANDLE CAN NEVER BE CHANGED TO POINT TO ANOTHER OBJECT.However the object can be modified.This restriction includes arrays which are also objects. At the point of declaration the final variables can be left blank,but they need to be assigned to finals either with an expression at the point of definition of the field or in every constructor. You can't actually have the method body inside an interface. The variable inside an interface are static and final.They can't be public/protected.
|
Ajith Kallambella bartender |
posted May 18, 2000 11:26 AM
Surya, Here are some small corrections to your list. Feel free to confront me if you don't agree with me
True only if the default appears after all case statements. Consider the following code
Here, because there is no break after the default, even case 3 statement gets executed.
I don't quite understand when you say variable definitions scattered through
What you said about final objects is not quite true. Just like final primitive
Infact you got the next point quite right. Remember the same applies to Object types also. You said-
[This message has been edited by Jim Yingst (edited May 24, 2000).]
|
Surya B ranch hand |
posted May 18, 2000 12:01 PM
Please correct me if i am wrong: Order of Initialization variables will be initialized depending on the order that the variables are defined within the class.Even if the variable definitions are scattered thru out method definitions,the variables are initialized before any methods can be called....even the constructor Please look at the example(From Thinking in Java) class Tag { class Card { public class OrderOfInitialization { will print out : In switch...case after default you can have a break or you can ignore it,either way its ok. You are right here, When using final with primitives final makes the value a constant,but with an ....etc etc Here what i meant was that the handle can never be changed to point to another object,and you are right when you said that you can initialize it any time after declaration but the object can be modified though you cant change the handle. Please look at the example(From Thinking in Java) public class ijk for(int i=0;i for(int i=0;i } The last one was a typo....sorry
|
Lt Abhi unregistered |
posted May 24, 2000 12:00 AM
Ajith, can you give me some idea about using System.exit() in try-catch-finally block. what happens when inside a catch block u use that
|
Ajith Kallambella bartender |
posted May 24, 2000 07:49 AM
The idea is, the finally block in a try-catch-finally always gets executed, no matter what. Even if you include a return value in catch, it will be stored, the finally block gets executed and then the value will be returned to the calling method. The only exception to this rule is the Consider the following example to make things little clearer
As you will observe, though the catch block has a return statement, the control will not return immediately. It will execute the finally block and then return the value 10. Hope this helps. [This message has been edited by Ajith Kallambella (edited May 24, 2000).]
|
sandra unregistered |
posted May 24, 2000 06:33 PM
Ajith, I am referring to your statement,"continue and break must be in a loop( for, do , while ). It cannot appear in case constructs". But break is used in case constructs. Am I missing something? sandra
|
Ajith Kallambella bartender |
posted May 24, 2000 06:41 PM
Yep. That was a bummer . Fixed it. Thanks for noticing! Ajith
|
viveksri unregistered |
posted May 24, 2000 06:49 PM
Hi to all, I just want want to know what is this JLS and can i get it.
|
Ajith Kallambella bartender |
posted May 24, 2000 07:37 PM
JLS means Java Language Specification. It describes the various aspects of the Java Language from the language designer's perspective. JLS should be used as an authentic documentation any time you start to wonder why something behaves the way it does. You can read JLS online here Enjoy reading..
|
pmj unregistered |
posted May 25, 2000 01:35 AM
Some points regarding inner classes Please correct me if I am wrong.
|
maha anna bartender |
posted May 25, 2000 08:26 AM
pmj, yes. you are right. Just to make it clear, your 3rd point is as such correct. Inner classes defined inside any method can access all the var including private/final vars which are available to the method within which the inner class is defined. In other words, if a inner class is inside a static method, it can access onty static final/private vars of the enclosing class. If the method would have been an non-static(instance) type method, then all the vars both static and non-static are available to the method and hence available to the inner class defined inside this method. You may know this already. Just to make your statement a complete one. Please refer to this thread to get some more interesting Java food in this context. [This message has been edited by maha anna (edited May 25, 2000).]
|
viveksri unregistered |
posted May 25, 2000 10:36 AM
Thanks Ajith.
|
satya5 ranch hand |
posted May 26, 2000 11:24 AM
Another URL with lots of one liners to remember for the exam From a previous post by Hari P. Regds. - satya
[This message has been edited by satya5 (edited May 26, 2000).]
|
Prem unregistered |
posted June 11, 2000 07:48 PM
Ajith, Can you please explain me the following trap of yours! Two public classes in the same file. I am following JavaRanch for about a month. I was bit confused on this. Thanks.
|
fan mo greenhorn |
posted June 11, 2000 10:58 PM
This thread is very useful. But I have some questions on maha's 1st post here. 1.Methods with the same name as the constructor(s). I tried running a test. It's OK to have a method whose name is same as constructors. code: 2. constructors can be private. refer to code above. Ajith and other's contribution are very good. I am waiting
|
Junaid Bhatra greenhorn |
posted June 28, 2000 12:04 AM
There is a slight correction...non-static inner class can have static variables, provided they are also declared final.
|
sgwbutcher ranch hand |
posted June 28, 2000 11:35 PM
Thanks to everyone for your contributions. As I have not taken the test yet, I dont' have any pearls to contribute but assuming I pass (and if I don't) perhaps I will... I have one question. My compiler says this following statement isn't true:
quote: Can someone check this? reference to post below Steve Butcher [This message has been edited by sgwbutcher (edited June 30, 2000).]
|
Junaid Bhatra greenhorn |
posted June 29, 2000 09:16 PM
The following code compiles and runs on my pc: class InnerTest2 public static void main(String[] args) So you can have static final variables inside a non-static inner class.
|
meadowlark b unregistered |
posted July 01, 2000 01:03 PM
It appears that JLS refers to the original Java and includes a link to a revision for Java 1.1. But where does that leave us regarding Java 2? Is the amendments suppose to contain all the differences? If so it seems like a short list. What about Java 2?
quote:
|
Junaid Bhatra greenhorn |
posted July 01, 2000 02:11 PM
You can find the draft version of JLS 2nd Ed in PDF format here:
|
Khalid Bou-Rabee ranch hand |
posted July 13, 2000 09:25 AM
Here are some additional tips... 1. You can use yield, sleep(int) and wait() for yielding other threads Remember, I am only hunam! I said earlier: The Garbage Collector Thread hs a low priority. Ajith, you are completely right. The Garbage Collector is unique to the Virtual Machine that you are using. ------------------ [This message has been edited by Khalid Bou-Rabee (edited July 13, 2000).]
|
Ajith Kallambella bartender |
posted July 13, 2000 09:48 AM
FanMo, The points listed( that you have noticed ) are actually things to look out for. 1.Methods with the same name as the constructor(s). Means watch out for methods with the same name as the constructors. They might mislead you in the exam!! Similarly, look out for private constructors. They are perfectly valid, and most likely to get you confused. As for inner classes with static members, I am sure I was right. Try it out yourself, No inner class can have a static member, whether the class itself is declared static or not!. I am not sure about Juniad Bhatra's post about final static variables, when I tried it out, I got a compiler error saying [b]"Only members of Interfaces and Top-level classes can be static" . However, when you declare and initialize the final static variable, it compiles fine. Sorry for the late reply, I was on an extended vacation. Ajith
|
Ajith Kallambella bartender |
posted July 13, 2000 09:52 AM
Khalid, Your statement 4. the Garbage collection thread has a low priority has to be taken with a pinch of salt. Almost nothing can be assumed about Garbage collection as it is vendor dependent. The only thing you can assume is every unreachable object will eventually become eligible for garbage collection. Ajith
|
Khalid Bou-Rabee ranch hand |
posted July 13, 2000 10:10 AM
Ajith, You said : "No inner class can have a static member, whether the class itself is declared static or not!" So is it resolved that an inner class, even if it is declared static, cannot declare any static members? But this code compiles and runs just fine:
-- My Java -- [This message has been edited by Khalid Bou-Rabee (edited July 13, 2000).]
|
Ajith Kallambella bartender |
posted July 13, 2000 11:11 AM
Khalid, Thank you for the code snippet. In your example, the class LittleSample is not an inner-class, but a top-level nested class. Perhaps I should have made my statement more precise - Is it clear now? Ajith
|
Ajith Kallambella bartender |
posted July 13, 2000 11:34 AM
Here is a summary of types of classes code: Hope this helps! [This message has been edited by Ajith Kallambella (edited July 13, 2000).]
|
Jim Yingst sheriff |
posted July 13, 2000 11:43 AM
A couple addenda to Ajith's list: static member classes used to be called "top-level nested classes", and were considered top-level, at least according to the original Nested Classes Specification. However this never really caught on, and under the JLS 2nd edition they are no longer considered top-level. I don't think any exam questions ever considered them top-level (or at least, they don't ask about it), so you're safe here. However, the exam does apparently consider static member classes to be inner classes, even though this is clearly contradicted by the Nested Classes Specification and the JLS2. They don't make a big deal about it - they just call them "static inner classes". So, for the exam, pretend they're inner classes, and remember that in the real world, they aren't.
|
Junaid Bhatra greenhorn |
posted July 13, 2000 12:09 PM
Hi Ajith, Inner classes can contain static fields provided they are also declared final (compile-time constants). This is clearly specified in the 2nd edition of JLS.
|
Meadowlark_B unregistered |
posted July 18, 2000 06:30 PM
quote: Does anyone know of any more links to lists like this? I'll be taking my exam tomorrow morning and these lists are an extremely valuable resource for last minute study. Thank you all by the way for putting this together. -Meadowlark Bradsher
|
Aru unregistered |
posted July 19, 2000 12:33 PM
Hi All, If I run this piece of code...given by Khalid, I'm getting this error...Can anyone Help. Exception in thread "main" java.lang.VerifyError: Thx in Advance import java.io.*; class Sample { LittleSample() { }
|
| | |