|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-28-2008, 11:16 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 9
|
|
|
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
|
|

06-28-2008, 11:20 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
|
|
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.
int weight;
double distanceRuns;
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-28-2008, 11:52 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 9
|
|
|
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?
|
|

06-28-2008, 12:03 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-28-2008, 12:13 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 9
|
|
yes , I know I just needed some tips on what to search on google  , now I will start looking for primitive type
|
|

06-28-2008, 12:37 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,477
|
|
|
Oh dear, you are asking how to Google. You man...
Java primitive data types/variable, access modifiers, and so on......
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-28-2008, 07:47 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 337
|
|
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.
private String fname;
public void setFirstName(String aFname) {
fname = aFname;
}
|
|

06-28-2008, 08:20 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 563
|
|
|
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.
|
|

07-01-2008, 07:13 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 9
|
|
I' m using NETBeans 6
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 .
|
|

07-02-2008, 12:31 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 152
|
|
|
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-02-2008 at 12:37 AM.
|
|

07-02-2008, 03:48 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 199
|
|
|
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.
|
|

07-02-2008, 04:39 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 337
|
|
|
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
|
|

07-02-2008, 05:09 PM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 9
|
|
Thanks Serjant, very usefull 
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  . 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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|