Results 1 to 11 of 11
  1. #1
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default Please explain these 2 lines of code to me..

    I'm working my way through the Sun Java Tutorials and I have come a cropper with what should be a simple section, initialisation of fields;

    There is an alternative to static blocks—you can write a private static method:
    class Whatever {
    public static varType myVar = initializeClassVariable() ;
    private static varType initializeClassVariable( ) {
    // initialization code goes here
    }
    }
    Is this two versions of the same thing? Or are both lines required to implement this? I'd appreciate a simple explanation of what's happening in the above 2 lines of code, cheers everyone.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    You are initialising a static variable named 'myVar' with the return value of a method named 'initializeClassVariable' which is defined on the next line.

    kind regards,

    Jos

  3. #3
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks JosAH, I am kicking myself for not seeing that. Thanks again. Sometimes the simplest things......

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    This might just be because you pasted your code on the web -- but indentation and whitespace really helps spot errors and see structure. I can't tell you how many times indentation has saved me from a stupid mistake - since when code is formatted, errors actually *look* wrong.

    Java Code:
    public static varType myVar = initializeClassVariable();
    
    private static varType initializeClassVariable( ) {
         // initialization code goes here
    }

  5. #5
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default

    Another quick one hopefully, what is going on in the line marked below? Cheers again folks.
    Java Code:
    public class RectanglePlus implements Relatable {
      public int width = 0;
      public int height = 0;
      public Point origin;
    
      // four constructors
      public RectanglePlus() {
        origin = new Point(0, 0);
      }
      public RectanglePlus(Point p) {
        origin = p;
      }
      public RectanglePlus(int w, int h) {
        origin = new Point(0, 0);
        width = w;
        height = h;
      }
      public RectanglePlus(Point p, int w, int h) {
        origin = p;
        width = w;
        height = h;
      }
    
      // a method for moving the rectangle
      public void move(int x, int y) {
        origin.x = x;
        origin.y = y;
      }
    
      // a method for computing the area of the rectangle
      public int getArea() {
        return width * height;
      }
    
      // a method to implement Relatable
      public int isLargerThan(Relatable other) {
        RectanglePlus otherRect = (RectanglePlus)other;  //<---Please explain this line, thanks!
        if (this.getArea() < otherRect.getArea())
          return -1;
        else if (this.getArea() > otherRect.getArea())
          return 1;
        else
          return 0;
      }
    }

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    You are casting the object of type Relatable to an object of type RectanglePlus.

    Basically you are saying this Relatable is actually a RectanglePlus object so you can use the RectanglePlus methods.

  7. #7
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default

    Does this other object have to conform to anything? I mean, I presume I can't cast a string as a rectangle etc. What determines what can be cast to something else? The java tutorials are good, but they sort of slip this in with no explanation. Cheers for the help.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,311
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by murphaph View Post
    Does this other object have to conform to anything? I mean, I presume I can't cast a string as a rectangle etc. What determines what can be cast to something else? The java tutorials are good, but they sort of slip this in with no explanation. Cheers for the help.
    The object has to be an instantiation of the RectanglePlus class or a subclass thereof. Casts don't change objects, they just check if the target class (where you're casting to) lies on the same path of the hierarchy starting with the Object class and the class you are casting from.

    kind regards,

    Jos

  9. #9
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks guys, the help is genuinely appreciated!

  10. #10
    murphaph is offline Member
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Default another quickie...

    Java Code:
    int indexOf(int ch, int fromIndex)
    int lastIndexOf(int ch, int fromIndex)
    and the description of the above from the java tutorials is:
    Returns the index of the first (last) occurrence of the specified character, searching forward (backward) from the specified index.
    My bold. I understand why the index is passed in as an int, but why is the specified character not of type char?

    Cheers for your patience.

    Edit: Is this a simple missprint?
    Last edited by murphaph; 01-19-2010 at 01:31 PM.

  11. #11
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    To search for the first occurrence of a character, use int indexOf(int ch)
    To search for the last occurrence of a character, use int lastIndexOf(int ch)

    indexOf(int ch, int fromIndex) Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

    ch - a character.
    fromIndex - the index to start the search from.


    So, if i hav a string lets say

    String str = "CBANICBANI"

    and if i do a str.indexOf(3, A);

    would return you 7...

Similar Threads

  1. Can someone explain why...
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2009, 06:59 AM
  2. how does this code work...explain me the execution please...
    By vital parsley in forum New To Java
    Replies: 3
    Last Post: 07-25-2008, 04:50 AM
  3. Removing empty lines from code using Eclipse
    By javaplus in forum Eclipse
    Replies: 1
    Last Post: 12-14-2007, 09:21 PM
  4. need to explain this code
    By reached in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 10:01 PM
  5. Unify these 3 lines of code
    By ulykidjoe in forum Advanced Java
    Replies: 4
    Last Post: 07-13-2007, 01:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •