View Full Version : Data Type Question
WildChild
12-14-2006, 05:12 PM
I'm abit new to java so excuse the simple question.
If I was asked to create a declaration for each of the following:
- a field of data type double and name radius
- a local variable of data type String and name address
Would this be right:
- public double radius
- private String address
also how do you determine whether to use private or public?
Whats the difference between a field data type and a local variable data type?
could someone give me an example?
brad211987
12-14-2006, 05:57 PM
Typically, a "local" variable is a variable that is used within a method, and the public/private/protected declaration is not needed. Something like this will do just fine.
double num = 0;where you declare the local variable num, give it the type double, and initialize its value to 0.
You declare a variable as private/public/protected when it is an instance variable of the class. These are outside of any methods and the scope of the variable(meaning where it can be accessed from) is determined by how you set the access. public access means anyone that creates an instance of that class can access the variable, for example if you have:
public class Test
{
public double num; //This is the instance variable
public static void main(String[] args)
{
double num2; //This is the local variable
}
}
In the above class, anyone can access the variable num, from their own class by creating a Test object and calling the variable like this:
Test testObject = new Test();
testObject.num = 5;
This is usually considered bad practice, because it does not hide the classes internal data from other classes. This is why instance variables are typically declared as private, where they can only be accessed from within the class in which they are declared. There is also protected access, in which the variable can be accessed within its own class, and by any other classes in the same package as the class containing the variable.
Hope that helps a little bit.
WildChild
12-14-2006, 06:11 PM
That's great thanks very much, what is a skeleton declaration?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.