Results 1 to 7 of 7
  1. #1
    Newjav13 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Initializing Private Classes.

    Im currently reading "The beginners guide to android game development" and the book does not explain Initializing private classes.

    When
    How
    Why
    How does it work!?

    I'm sure that it explains it perfectly to some people but I have read over it at least 10 times and still I am lost. I get that since its a private class we have to do something to make it known in the main class but I just really need to understand how and where to use this. I know its an extremely important tool.

    If someone can post a detailed description with an image I would greatly appreciate it. Until then I am at a stand still in my book, I don't want to continue past something I don't understand at all.

    Thank you in advance!!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Initializing Private Classes.

    Do you have any coding samples of what you are asking about?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Newjav13 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Re: Initializing Private Classes.

    Quote Originally Posted by Norm View Post
    Do you have any coding samples of what you are asking about?
    Yeah, Sorry about that! I'm not necessarily trying to code this. Its just an example in the book about make parts of programs private. Which will come in handy when I start creating games. Things like, usernames and passwords, scoring systems and all that should be private so people cant just hack in and make themselves the highscore.

    But here is the example!


    public class Phone {


    private boolean poweredOn;
    private boolean playingMusic;
    private String phoneManufacturer;
    private double androidVersionNumber;

    //these methods are the phones objects behaviors
    void initialize(boolean poweredOn, String phoneManufacturer){
    this.poweredOn = poweredOn;
    this.phoneManufacturer = phoneManufacturer;

    }

    void togglePower() {
    if (poweredOn) {
    System.out.println("Powering Off");
    poweredOn = false;
    playingMusic = false;

    }else{
    System.out.println("Powering On!");
    poweredOn = true;
    }
    } //ends togglePower method

    void playMusic () {
    if (poweredOn) {
    System.out.println("Playing music!");
    playingMusic = true;
    }
    }//ends playingMusic method

    void upgrade (double newVersion) {
    if(newVersion > androidVersionNumber) {
    androidVersionNumber = newVersion;
    }else{
    System.out.println("Upgrade failed!");

    }




    }

    void describe() {
    System.out.println("\nmyPhone's NEW state:");
    System.out.println("Powered on: " + poweredOn);
    System.out.println("Playing Music:" + playingMusic);
    System.out.println("Manufacturer: " + phoneManufacturer);
    System.out.println("Version: " + androidVersionNumber);
    }
    }

  4. #4
    Newjav13 is offline Member
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Re: Initializing Private Classes.

    The part that confuses me is he only initialized two methods.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Initializing Private Classes.

    part that confuses me is he only initialized two methods.
    Sorry, that doesn't make any sense. methods are defined. They don't have values that can be initialized.
    Variables like Strings and ints have values that can be initialized.

    Please edit your post and wrap the code in code tags to preserve its formatting and make it easier to read.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Initializing Private Classes.

    Quote Originally Posted by Newjav13 View Post
    The part that confuses me is he only initialized two methods.
    That can probably be translated to: Why does the method initialize() set the value of only two of the four class properties?

    Answer: because apparently the default values of the other two properties are good enough to begin with. For the real answer, probably read more in that book of yours. Single snippets are usually not the whole truth, a concept is generally explained in several intermediate steps, each building on the previous step.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Mosi is offline Member
    Join Date
    Jul 2015
    Posts
    1
    Rep Power
    0

    Default Re: Initializing Private Classes.

    Hello Newjav13
    Normally you program for each private variable a specific function to get or set the variable.
    You can also set the variable via the constructor of the class. In your example I didnít see a constructor.
    But I think the variable androidVersionNumber and playingMusic is set in a constructor.
    The constructor is called when the object is generated. In this case it makes sense to set playingMusic to false and the androidVersionNumber to a default value.

    If you programme a class Phone you should add a constructor to it. I recommend that you initialize all the variables with a default value in this constructor.
    The constructor has always the same name as the class. In this example: public Phone(parameter1,parameter2Ö).
    You can program more than one constructor. The difference are the number of parameters.

    Best regards,
    Stefan

Similar Threads

  1. Question on Singleton + Private Inner Classes
    By fatabass in forum New To Java
    Replies: 5
    Last Post: 09-11-2012, 10:45 AM
  2. Replies: 1
    Last Post: 02-10-2011, 04:43 AM
  3. OOP Question re. private variables and extending classes
    By ImplicitCharm in forum New To Java
    Replies: 7
    Last Post: 07-28-2009, 03:46 PM
  4. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM
  5. Private Classes Clarification
    By justlearning in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 10:51 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
  •