Results 1 to 15 of 15
  1. #1
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default 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:
    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.
    }
    Same for methods etc, not just instance variables. Or?
    Last edited by überfuzz; 06-02-2011 at 10:56 AM.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  3. #3
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Al right! So if I were to make some program calculating stuff I could do:
    Java Code:
    public static final double pi = 3.14;
    final to prevent over writing it.
    static to make every object sharing pi.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    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

  5. #5
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    I know, I just had to clarify the use of static final etc. :-)

  6. #6
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    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.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Quote Originally Posted by überfuzz View Post
    I know, I just had to clarify the use of static final etc. :-)
    By the way, static final are usually used when writing constant values. As per naming convention, that variable should be upper cased. Something like,

    Java Code:
    public static final double PI = 3.141592653589793;
    Refer to naming convention here : Naming Conventions

    Just an FYI as the exam is coming up

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    static final are usually used when writing constant values
    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 Block
    Skype: petrarsentev
    http://TrackStudio.com

  9. #9
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Quote Originally Posted by Petr View Post
    I read that now to make so is not advice instead it you can use enumerations.
    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.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by Petr View Post
    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 Block
    PI as part of an enum makes no sense...

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

    Default

    Quote Originally Posted by DarrylBurke View Post
    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.
    As all the religious people know that value equals 3; that truth can be found (coincidentally) in Kings 22:7 (<--- here!)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Constants written in capitals. I'll keep that in mind.

    I'm brooding some things... about classes.
    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?
      }
    }
    I hope I'm making some sense here, baer with me.
    Last edited by überfuzz; 06-02-2011 at 05:05 PM. Reason: Made the code a bit tidier

  13. #13
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Suggested reading : Abstract Methods and Classes and Defining Methods

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    As all the religious people know that value equals 3; that truth can be found (coincidentally) in Kings 22:7 (<--- here!)

    kind regards,

    Jos
    1 Kings or 2 Kings?

    Oh, I see. I think you missed the i in Josiah

    db

  15. #15
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Exam today, I think I did all right.
    My thanks to everyone who pitched in!

Similar Threads

  1. Jax is coming to the us!!!
    By blease87 in forum Reviews / Advertising
    Replies: 0
    Last Post: 03-04-2011, 01:21 PM
  2. XML, and other things.
    By Tortex in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 06:53 PM
  3. Please help output not coming
    By majin_harish in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:45 AM
  4. Why is the answer not coming out
    By anonymous18 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 04:10 AM
  5. How can i know from which jsp request is coming?
    By vishnujava in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2008, 02:13 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
  •