Results 1 to 13 of 13
  1. #1
    littleBean is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default simple question about private data

    Hi everyone !
    i'm very new to java and I found a this source ( the one below) in internet , but I ' ve a question.

    class Test
    {
    private String surname;
    private String name;
    private String preferredcolor;
    private int age;

    public Test(String psurname,String pname,String ppreferredcolor,int page)
    [....]
    }

    why did the compiler use "p" before every variable? is the "p" required, every time I declare a private variable? or he could have written simply (String surname, String name, and so on ?
    thanks

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes you can, there is no point to use a such a way. Normally in Java use camel case in naming conversions for variables. That is first letter is simple, and if you want to use more than one word after the first word, all first letter is capital in each word.

    Java Code:
    int weight;
    double distanceRuns;

  3. #3
    littleBean is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    ah, so "private String surname" and "public Test( String psurname) " are not connected with each other? they are 2 different variable, not only one that has been used first as private and then as public. Then we can even write

    "private String surname" and "public Test( String tree)" and the meaning of the source will not change, isn't it?

    another question : why the private variables are one for line, and instead the public ones are all grouped?is it a choice of the compiler or a stardard of java?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    For the firs question, surname and psurname are two different variables. There is no any connection at the time of your code.

    private, public are called access modifiers in Java. Basically it define the accessibility of specific code segment.

    Depend on the access modifier you can't group the variables, only you can grouping depends on the primitive types.

    You have better to refer some documentations on those things before moving in deep of Java. Explaining on a forum is not easy at all.

  5. #5
    littleBean is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    yes , I know I just needed some tips on what to search on google :D, now I will start looking for primitive type

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Oh dear, you are asking how to Google. You man...

    Java primitive data types/variable, access modifiers, and so on......

  7. #7
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Java is not C circa 1989. There is no reason to use Hungarian notation, per Microsoft's early Windows SDK.

    There is plenty of reasons to avoid Hungarian.

    I ususaly make the function parameters' names be related to their use, but not exactly the same as the private members.

    Java Code:
    private String fname;
    
    public void setFirstName(String aFname) {
      fname = aFname;
    }

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Smile Start simple.

    Little Bean, you are very likely using a tool called an IDE - Integrated Development Environment. Those are very useful but for total beginner leaves you studying the IDE rather than Java. Those become a study of their own.

    Have you read any books? Those can be checked out from the library.

  9. #9
    littleBean is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    I' m using NETBeans 6
    Have you read any books?
    do you mean about java? I'm reading one and I' m trying to understand here the things I' m not sure of . Another one question , i wrote this class:

    class Piano{
    static public double calc(double x1, double y1, double x2, double y2 ){
    double z = (x1 -x2)*(x1-x2) + (y1-y2)*(y1-y2);
    System.out.println(Math.sqrt(z));
    return z;
    }}

    to calculate distance between two point of cartesian plane, but if I delete the "return z" string , NETBean say that I missed to insert the returned type.
    But I 've already declared the type(double) of z , I know return is used to get the result of the operation and return it to the program, but I don' t understand why is necessary here since I' ve only asked to print a line , not to use the result for something else .

  10. #10
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Did you read about "void" and "return" methods,and how they work?
    So,when you wanna only print the square root of z so use "public void nameOfMethod(@params)" to declare the method,but if you want just to make the method which will return some type of variable and then somewhere you will use it so declare as "public..." +what type of variable or object you want to return+"nameOfMethod(@params)"
    Last edited by serjant; 07-01-2008 at 11:37 PM.

  11. #11
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    6

    Default

    hello little bean, may i suggest that you first try your codes using a text editor and compile it manually before you use a IDE. Refer to some books, most of them will teach you to compile and code java programs using the commandline and a text editor. ^_^ Start from the Basics
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  12. #12
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    OK so @eku and I disagree, reasonable folks can disagree.

    I like an IDE for rookies learning because it hides a lot of complexity. You type in your code, push the run button and it tries to run. Push the debug button, and step into your code to see why you have bugs.

    I can see how using vi or emacs and a command line compiler can bring some special knowledge. I just think that things like classpath etc just seem opaque to folks at first

  13. #13
    littleBean is offline Member
    Join Date
    Jun 2008
    Posts
    9
    Rep Power
    0

    Default

    Thanks Serjant, very usefull:D
    I ' m reading a book , otherwise I could not write this simple class from nothing, but books don' t speak and if I don' t understand something, I can't ask to him and not always google help like a forum can do . It ' s just this:rolleyes:. For the moment I feel more sure programming with an IDE rather than using a notepad, for the simple reason that , for now program correct my syntax so that I can learn it well and reusing it in a notepad.

Similar Threads

  1. Simple Method Question
    By Froz3n777 in forum New To Java
    Replies: 2
    Last Post: 02-13-2008, 02:39 AM
  2. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 01:27 AM
  3. Simple question of JTable
    By carl in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:07 AM
  4. Question of private member
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:08 PM
  5. oracle query with date question
    By orchid in forum JDBC
    Replies: 1
    Last Post: 05-09-2007, 12:10 AM

Posting Permissions

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