A few Java Questions
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Henry_Leung
Posted On:   Saturday, December 7, 2002 05:23 PM

1. In C++, a class can allow another particular class, regarded as "friend class" to access its own private members. Can Java class do this? 2. In C++, when we declare a function, we can set some default values for the arguments of the function. If those arguments are not supplied during a function call, those arguments will be set to default values. Can Java method do this? 3. In C++, we can declare a kind of variable that can only be assigned to serveral values. For example, I can declare a variable called Weekdays. This Weekdays variable can only equal to one of the Strings "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", and "Sun". If you assi   More>>

1. In C++, a class can allow another particular class, regarded as "friend class" to access its own private members. Can Java class do this?



2. In C++, when we declare a function, we can set some default values for the arguments of the function. If those arguments are not supplied during a function call, those arguments will be set to default values. Can Java method do this?



3. In C++, we can declare a kind of variable that can only be assigned to serveral values. For example, I can declare a variable called Weekdays. This Weekdays variable can only equal to one of the Strings "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", and "Sun". If you assign it to other values, the compiler will show an error. Can Java do this??


   <<Less

Re: A few Java Questions

Posted By:   Joe_Cheng  
Posted On:   Monday, December 9, 2002 01:01 PM

#3: Typesafe enums may be coming to Java with release 1.5, and generics too.

Re: A few Java Questions

Posted By:   Andreas_Wolf  
Posted On:   Saturday, December 7, 2002 11:57 PM

1. I'm afraid: No. Access to class members in Java can be only one of private (only the class itself can access them - not even child classes can), protected (all classes in the same package can access them) and public (everybody can access them).



2. Default values for arguments as well as the ellipsis
(...) declarator for variable length parameter sets are
available in C++, but not in Java. Again: No.



3. There are no "enums" in Java itself, but you can
easily create typesafe collections of values using the
following technique:


class Weekday {
public static final Weekday mon = new Weekday();
public static final Weekday tue = new Weekday();
private Weekday() {}
}


Declaring the constructor as private, variables of type
Weekday can only be assigned (and thus only have a defined set of values), using Weekday.mon, Weekday.tue, etc...

About | Sitemap | Contact