Results 1 to 15 of 15
- 06-02-2011, 09:51 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Exam coming up - Need to check some things
I'm trying to grasp what some stuff do.
public - this makes a class,method etc. open to the world, so to speak.
private - open to class it self.
static - info shared by all classes or methods.
Please comment this:
Same for methods etc, not just instance variables. Or?Java Code:public class Object{ private var_1; //only visible within the class. public var_2; //open to all classes public static var_3 //open to all classes, all objects of type Object share this info. private static var_4 //only visible within the class, all objects of typ Object share this. }Last edited by überfuzz; 06-02-2011 at 09:56 AM.
- 06-02-2011, 10:13 AM #2
It is access modifiers like public > protected > default > private. see Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
static is keyword for is used without a instance object.
final is keyword. It declare that variable can't re-instance.Skype: petrarsentev
http://TrackStudio.com
- 06-02-2011, 12:36 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Al right! So if I were to make some program calculating stuff I could do:
final to prevent over writing it.Java Code:public static final double pi = 3.14;
static to make every object sharing pi.
- 06-02-2011, 12:49 PM #4
Yes, but that's a terrible value for π. And the java.lang.Math class already defines a much, much closer approximation that you can use.
db
- 06-02-2011, 01:07 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
I know, I just had to clarify the use of static final etc. :-)
- 06-02-2011, 01:21 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
By the way, this is all stuff I should know... :-)
The course is mostly recursive methods, Linked Lists, Binary threes, etc. Quite tricky to get it right, as we're writing code on paper. With the comp you always gets an error throned in your face if you mess things up.
- 06-02-2011, 01:56 PM #7
By the way, static final are usually used when writing constant values. As per naming convention, that variable should be upper cased. Something like,
Refer to naming convention here : Naming ConventionsJava Code:public static final double PI = 3.141592653589793;
Just an FYI as the exam is coming up
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-02-2011, 02:07 PM #8
I read that now to make so is not advice instead it you can use enumerations. I read it in a book Effective Java by Johnny Blockstatic final are usually used when writing constant valuesSkype: petrarsentev
http://TrackStudio.com
- 06-02-2011, 03:41 PM #9
It depends on your requirement and design. I mean, if you have only a single or couple of constants then you can declare them simply the usual way.
You can have enums when the constants are related. Or you can even have your constants declared in the interface and then you can use them by implementing it. There may be other ways as well.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-02-2011, 03:54 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
- 06-02-2011, 04:03 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,404
- Blog Entries
- 7
- Rep Power
- 17
- 06-02-2011, 04:03 PM #12
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Constants written in capitals. I'll keep that in mind.
I'm brooding some things... about classes.
I hope I'm making some sense here, baer with me.Java Code:public abstract class Main{ int instance_variable Object something; public Main(Object something){ //constructor this.something = something; this.instance_variable = 10; } public someMethod(){ return instance_variable; } } public class Sub extends Main{ public Sub(Object something){ super(something); //free loading mains constructor. //I can write more constructors here as long as I don't use Sub(Object), already taken. } public int someMethod(){ //Is it possible to leave out methods that's situated in the Main class, even if main is abstract? //Or do I have to write then here as well? } }
Last edited by überfuzz; 06-02-2011 at 04:05 PM. Reason: Made the code a bit tidier
- 06-02-2011, 04:27 PM #13
Suggested reading : Abstract Methods and Classes and Defining Methods
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-02-2011, 06:21 PM #14
- 06-03-2011, 09:31 PM #15
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Similar Threads
-
Jax is coming to the us!!!
By blease87 in forum Reviews / AdvertisingReplies: 0Last Post: 03-04-2011, 12:21 PM -
XML, and other things.
By Tortex in forum New To JavaReplies: 5Last Post: 03-28-2010, 05:53 PM -
Please help output not coming
By majin_harish in forum New To JavaReplies: 2Last Post: 02-05-2009, 09:45 AM -
Why is the answer not coming out
By anonymous18 in forum New To JavaReplies: 4Last Post: 11-12-2008, 03:10 AM -
How can i know from which jsp request is coming?
By vishnujava in forum Java ServletReplies: 1Last Post: 08-06-2008, 01:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks