Results 1 to 11 of 11
- 01-13-2010, 05:09 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
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;
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.There is an alternative to static blocksyou can write a private static method:
class Whatever {
public static varType myVar = initializeClassVariable() ;
private static varType initializeClassVariable( ) {
// initialization code goes here
}
}
- 01-13-2010, 05:13 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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
- 01-13-2010, 07:45 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
Thanks JosAH, I am kicking myself for not seeing that. Thanks again. Sometimes the simplest things......
- 01-13-2010, 09:36 PM #4
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 }
- 01-14-2010, 03:10 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
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; } }
- 01-14-2010, 03:45 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.
- 01-14-2010, 05:05 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
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.
- 01-14-2010, 05:11 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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
- 01-14-2010, 05:27 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
Thanks guys, the help is genuinely appreciated!
- 01-19-2010, 01:28 PM #10
Member
- Join Date
- Jan 2010
- Posts
- 11
- Rep Power
- 0
another quickie...
and the description of the above from the java tutorials is:Java Code:int indexOf(int ch, int fromIndex) int lastIndexOf(int ch, int fromIndex)
My bold. I understand why the index is passed in as an int, but why is the specified character not of type char?Returns the index of the first (last) occurrence of the specified character, searching forward (backward) from the specified index.
Cheers for your patience.
Edit: Is this a simple missprint?Last edited by murphaph; 01-19-2010 at 01:31 PM.
- 01-19-2010, 02:11 PM #11
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
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
-
Can someone explain why...
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-19-2009, 06:59 AM -
how does this code work...explain me the execution please...
By vital parsley in forum New To JavaReplies: 3Last Post: 07-25-2008, 04:50 AM -
Removing empty lines from code using Eclipse
By javaplus in forum EclipseReplies: 1Last Post: 12-14-2007, 09:21 PM -
need to explain this code
By reached in forum New To JavaReplies: 3Last Post: 12-03-2007, 10:01 PM -
Unify these 3 lines of code
By ulykidjoe in forum Advanced JavaReplies: 4Last Post: 07-13-2007, 01:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks