• 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

Traps to be aware of in any SCJP test !!!!! :)

 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
regds
maha anna
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am posting this on behalf of Ajith.
Ajith's contribution
--------------------

  • Two public classes in the same file.
  • Main method calling a non-static method.
  • Methods with the same name as the constructor(s).
  • Thread initiation with classes that dont have a run() method.
  • Local inner classes trying to access non-final vars.
  • Case statements with values out of permissible range.
  • Math class being an option for immutable classes !!
  • instanceOf is not same as instanceof
  • Private constructors
  • An assignment statement which looks like a comparison if ( a=true)...
  • System.exit() in try-catch-finally blocks.
  • Uninitialized variable references with no path of proper initialization.

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my contribution
- Classes derived from abstract and not providing body for all abstract methods.
- Classes implementing interface and not providing body for all methods.
- Derived classes from Base classes without no arg (default) c'tor and implicitly invoking super().
- Math.floor() and Math.ceil() with -ive numbers.
- >>> operations on -ive byte primitives.

Originally posted by maha anna:
I am posting this on behalf of Ajith.
Ajith's contribution


 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carl's notes have a list of such gotchas:
<pre>Things to look out for

*** Read the answers before going through code samples.

****** LOOK FOR NON-STATIC METHODS/VARS ACCESSED FROM MAIN

****** WHEN YOU FINISH THE EXAM GO BACK AND CHECK FOR NON-STATIC
METHODS/VARS FROM MAIN AND OTHER STATIC METHODS

* if (...)
statement1;
statement2; //...always executed

* Beware of an otherwise legal override having a more restrictive
access modifier.

* Beware of missing return statements.

* Look for static modifiers and make sure they dont contain refs to
instance vars.

* Beware of standard methods (eg. main, paint, run) with the wrong
args or return type.

* With array declarations, look out for "int intArray[5] = ..."

* Beware of adding a primitive to a Vector.
- more generally, you can't use a primitive where an Object is
required (eg. the equals method).

* System.out.println( aReferenceToNull ); // is fine

* Beware of local vars from a try block used in its catch block
(out of scope).</pre>
[This message has been edited by Jim Yingst (edited May 17, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are some which I wrote so I SHOULD check before
entering the exam hall:

  • Variable/Method names cannot start with numbers.
    Acceptable are: chars, $ and _
    (Edited)
  • b += i; works (when the result is within range) even when
    b is a byte and i is an int.
  • constructors can be 'private'
  • annonymous class CANNOT have constructors. ( DONOT confuse
    this with abstract classes. abstract class MAY have
    constructors. example Component() )
  • int i = -456789; ~i = ? Use this short cut: ~i = (-i) -1;

  • If any of this is wrong, I am assuming the moderators will
    edit this message.
    Thanks.
    Suresh:
    Thanks for checking, I will have to get back
    to my notes and see why I had that sentence.
    I have removed the statement.
    - satya
    [This message has been edited by satya5 (edited May 17, 2000).]
    [This message has been edited by satya5 (edited May 18, 2000).]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
satya,
one small correction in your list.


  • int i; if (i = 2) ALWAYS evaluates to true.
    (basically a valid assignment in if() clause )


The above statement will give a compiler error stating:


Incompatible type for if. Can't convert int to boolean.


And, that's a great list Maha, Ajith, satya and Jim. Will be very useful for people like me, who make careless mistakes!!
Thanks!!
I am also searching my notes, if I can add some more to the list.
Regards,
Suresh.
[This message has been edited by Suresh (edited May 18, 2000).]
[This message has been edited by Suresh (edited May 18, 2000).]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha Anna
I have a doubt. What does "Math class being an option for immutable classes !!" mean?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jayashree,
Consider this question.
<PRE>
Which of the following represent immutable classes?
a) String b) Double
c) StringBuffer d) Math
</PRE>
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
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here goes some more.
  • Order of try-catch-finally blocks matters.
  • main() can be declared final.
  • -0.0 == 0.0 is true.
  • A class without abstract methods can still be declared abstract.
  • RandomAccessFile descends from Object and implements DataInput and DataOutput.
  • Map doesnot implement Collection.
  • Dictionary is a class, not an interface.
  • Collection is an Interface where as Collections is a helper class.
  • Class declarations can come in any order ( derived first, base next etc. ).
  • Forward references to variables gives compiler error.
  • Multi dimensional arrays can be sparce ie., if you imagine the array as a matrix, every row need not have the same number of columns.
  • Arrays, whether local or class-level, are always initialized,
  • Strings are initialized to null, not empty string.
  • An empty string is NOT the same as a null string.
  • A declaration cannot be labelled.
  • continue must be in a loop( for, do , while ). It cannot appear in case constructs.
  • Primitive array types can never be assigned to each other, eventhough the primitives themselves can be assigned. ie., ArrayofLongPrimitives = ArrayofIntegerPrimitives gives compiler error eventhough longvar = intvar is perfectly valid.
  • A constructor can throw any exception.
  • Initilializer blocks are executed in the order of declaration.
  • Instance initializer(s) gets executed ONLY IF the objects are constructed.
  • All comparisons involving NaN and a non-Nan would always result false.
  • Default type of a numeric literal with a decimal point is double.
  • integer (and long ) operations / and % can throw ArithmeticException while float / and % will never, even in case of division by zero.
  • == gives compiler error if the operands are cast-incompatible.
  • You can never cast objects of sibling classes( sharing the same parent ), even with an explicit cast.
  • <WrapperClass>.equals returns false if the object types are different.It does not raise a compiler error.
  • No inner class can have a static member.
  • File class has NO methods to deal with the contents of the file.
  • InputStream and OutputStream are abstract classes, while DataInput and DataOutput are interfaces.


  • More coming.....
    [This message has been edited by Ajith Kallambella (edited May 24, 2000).]
    [Edited by Val to correct the equals thing]
    [ March 22, 2002: Message edited by: Valentin Crettaz ]
     
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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,
    Ajith
     
    Sushma
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I thought you were talking about normal arrays.
    Thanks a lot for your points. They are very helpful.
    Sushma
     
    Ajith Kallambella
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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).]
     
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 {
    public static void main(String[] args) {
    for(int i = 0; i < 100; i++) {
    char c = (char)(Math.random() * 26 + 'a');
    System.out.print(c + ": ");
    switch(c) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    System.out.println("vowel");
    break;
    }
    }
    }
    }

    Each Overloaded method must take a unique list of argument types.Even differences in the ordering of arguments is sufficient to distinguish two methods.
    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 a[][]=new int[1][];---Is OK.
    int []d[][]=new int[1][][];---Is OK.

    If you want to define Multi-dimensional curly arrays then define like this:
    int c[][]={{1,2,3,},{4,5,6},};--Don't use the new int keyword.The comma after the last value is optional.

    If u don't put an access specifier to a class then it defaults to friendly and means that an object of that class can be created by any other class in the package but not outside the package.However if the static member of that class is public the client programmer can still access the static member even though they cannot create an object of that class.
    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
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Surya,
    Here are some small corrections to your list. Feel free to confront me if you don't agree with me

    You said -
    In switch...case after default you can have a break or you can ignore it,either way its ok.
    True only if the default appears after all case statements. Consider the following code
    <PRE>
    int i = 10 ;
    switch(i)
    {
    default : System.out.println('10');
    case 3 : System.out.println('3'); break ;
    case 9 : System.out.println('9'); break ;

    }
    </PRE>
    Here, because there is no break after the default, even case 3 statement gets executed.

    You said -
    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.

    I don't quite understand when you say variable definitions scattered through
    out method definitions. Variables defined within the scope of the method are
    DIFFERENT than the ones defined in the scope of the class. Are you referring to
    initializer blocks here?

    You said -
    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.

    What you said about final objects is not quite true. Just like final primitive
    variables, you can initialize it any time after declaration, before referencing
    it , ONLY ONCE. Take a look at this code to make this concept clearer -
    <PRE>
    class A {}
    class FinalObject
    {
    public static void main( String s[] )
    {
    final A aObj;
    System.out.println("Now initializing aObj");
    // Lots of code here, but cannot reference aObj yet!
    aObj = new A();
    }
    }
    </PRE>
    Infact you got the next point quite right. Remember the same applies to Object
    types also.
    You said-
    The variable inside an interface are static and final.They can't be
    public/protected

    Interface variables are implicitly static,public
    and final. They cannot be private or protected. Was it a typo??

    Ajith

    [This message has been edited by Jim Yingst (edited May 24, 2000).]
     
    Surya B
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 {
    Tag(int marker) {
    System.out.println("Tag(" + marker + ")");
    }
    }
    class Card {
    Tag t1 = new Tag(1);
    Card() {
    System.out.println("Card()");
    t3 = new Tag(33);
    }
    Tag t2 = new Tag(2);
    void f() {
    System.out.println("f()");
    }
    Tag t3 = new Tag(3);
    }
    public class OrderOfInitialization {
    public static void main(String[] args) {
    Card t = new Card();
    t.f();
    }
    }
    will print out :
    Tag(1)
    Tag(2)
    Tag(3)
    Card()
    Tag(33)
    f()
    In switch...case after default you can have a break or you can ignore it,either way its ok.
    You are right here,
    What i meant was the compiler won't complain if you didn't include the break after the case.
    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
    {
    final int a=1;
    final static int myStaticArray[]={1,2,3,4,5,6};
    static void change()
    {
    final int k=1;
    final int myArray[]={1,2,3,4,5,6};
    for(int i=0;i<myArray.length;i++)>
    {
    k=k+1;//Is not OK
    System.out.println("Before being initialized: "+myArray[i]);
    }
    for(int i=0;i<myArray.length;i++)>
    {
    myArray[i]=9999;
    System.out.println("After being re-initialized: "+myArray[i]);
    }
    for(int i=0;i<myStaticArray.length;i++)>
    {
    myStaticArray[i]=8888;
    System.out.println("After being re-initialized: "+myStaticArray[i]);
    }
    }
    public static void main(String[] args)
    {
    change();
    }
    }
    The last one was a typo....sorry
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    system.exit() , if when included, will STOP
    executing the rest of the code.
    Consider the following example to make things little clearer
    <PRE>
    class ExceptionDemo
    {
    public static void main( String[] s)
    {
    int i = ExceptionMethod();
    System.out.println( i ) ;
    }
    static int ExceptionMethod()
    {
    try
    {
    throw new NullPointerException() ;
    }
    catch( NullPointerException ex )
    {
    System.out.println("Caught NullPointerException");
    // If you uncomment the following line,
    // finally will not be executed, and no value will
    // be returned.
    //System.exit(0);
    return 10 ;
    }
    finally
    {
    System.out.println("I am invincible");
    }
    }
    }

    </PRE>
    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.
    However, if you uncomment the System.exit call in the catch block, the program terminates right there, causing finally block to be ignored, and also the return
    value.
    Hope this helps.
    Ajith

    [This message has been edited by Ajith Kallambella (edited May 24, 2000).]
     
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yep. That was a bummer . Fixed it. Thanks for noticing!
    Ajith
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi to all,
    I just want want to know what is this JLS and can i get it.
     
    Ajith Kallambella
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 http://java.sun.com/docs/books/jls/html/index.html
    You can also download JLS in the HTML format.
    Enjoy reading..
    Ajith
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Some points regarding inner classes
    Please correct me if I am wrong.

    Inner classes
    Can access all instance variables of enclosing classes including static,private vars
    Static Inner classes
    Can only access static methods /var of enclosing classes
    Inner classes inside methods can access can access only final variables of that method
    They can access non final even private vars of enclosing class.
     
    maha anna
    Ranch Hand
    Posts: 1467
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
    regds
    maha anna
    [This message has been edited by maha anna (edited May 25, 2000).]
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Ajith.
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Another URL with lots of one liners to remember for the exam
    here
    From a previous post by Hari P.
    Regds.
    - satya

    [This message has been edited by satya5 (edited May 26, 2000).]
    [This message has been edited by satya5 (edited May 26, 2000).]
     
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
    Prem

     
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.

    2. constructors can be private. refer to code above.
    3. No inner class can have a static member.
    maybe no-static inner class can have static members is more
    precisely.
    Ajith and other's contribution are very good. I am waiting
    for more to come.....
     
    Ranch Hand
    Posts: 213
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There is a slight correction...non-static inner class can have static variables, provided they are also declared final.
     
    Ranch Hand
    Posts: 56
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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:


    There is a slight correction...non-static inner class can have static variables, provided they are also declared final.


    Can someone check this?
    reference to post below
    Thanks Junaid...either my compiler has a bug or a feature was added between 1.1.8 and 1.2.2
    Steve Butcher

    [This message has been edited by sgwbutcher (edited June 30, 2000).]
     
    Junaid Bhatra
    Ranch Hand
    Posts: 213
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The following code compiles and runs on my pc:
    class InnerTest2
    {
    class InnerOne
    {
    static final int i= 99;
    }
    public static void main(String[] args)
    {
    System.out.println(new InnerTest2(). new InnerOne().i);
    }
    }
    So you can have static final variables inside a non-static inner class.
     
    Anonymous
    Ranch Hand
    Posts: 18944
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?

    Originally posted by Ajith Kallambella:
    You can read JLS online here http://java.sun.com/docs/books/jls/html/index.html
    You can also download JLS in the HTML format.
    Enjoy reading..
    Ajith[/B]



     
    Junaid Bhatra
    Ranch Hand
    Posts: 213
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can find the draft version of JLS 2nd Ed in PDF format here:
    http://java.sun.com/aboutJava/communityprocess/maintenance/JLS/index.html
     
    Ranch Hand
    Posts: 54
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here are some additional tips...
    1. You can use yield, sleep(int) and wait() for yielding other threads
    2. Floating numbers are only estimations! Thus, ((float)(2/3))*3 is not 2!
    3. You can perform >> on any primitive (although it is not wise to do it on primitives smaller than int because you may suffer sign change...)
    4. switch statements must be compatible with int, but this does not mean the value is widened, instead all the constant (or final) case options must fit within the variable's bounds.
    5. LayoutManager2 is an interface used for constraints with GridBagLayout, GridLayout and BordLayout.
    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.
    ------------------
    \\ //
    ~\// irucidal~

    [This message has been edited by Khalid Bou-Rabee (edited July 13, 2000).]
     
    Ajith Kallambella
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 54
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    <html>
    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:
    <pre>
    import java.io.*;
    class Sample {
    public static void main( String arg[] ) {
    System.out.println("I'm a little sample, short and ... ");
    Sample.LittleSample b = new Sample().new LittleSample();
    }
    static class LittleSample {
    static String str = "stout";
    LittleSample() {
    System.out.println(str);
    }
    }
    }
    </pre>
    -- My Java --
    java version "1.3.0"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-beta)
    Java HotSpot(TM) Client VM (build 1.3-beta, mixed mode)
    </html>
    [This message has been edited by Khalid Bou-Rabee (edited July 13, 2000).]
     
    Ajith Kallambella
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 -
    No inner class( except static nested )......
    Is it clear now?
    Ajith
     
    Ajith Kallambella
    Sheriff
    Posts: 5782
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here is a summary of types of classes

    Hope this helps!
    Ajith
    [This message has been edited by Ajith Kallambella (edited July 13, 2000).]
     
    Jim Yingst
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic