Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2006

Tip: Looking for answers? Try searching our database.

What it the type of the Class without modifier

Thread view: 
hon123456 - 20 Jan 2006 04:28 GMT
Dear all,

            I got a java file contains the following code:

            public class ABC {

                   ......
            }

            class Test {

                   .....
            }

    the class Test is without modifier, so , what is the type of class
for Test? Private?
    protected? What is the scope and behavior of class without
modifier?

Thanks
Torkel Franzen - 20 Jan 2006 06:02 GMT
>      the class Test is without modifier, so , what is the type of class
> for Test? Private?
>      protected? What is the scope and behavior of class without
> modifier?

 No modifier means default access - accessible to code in classes in the
same package.

 What would it mean for a top level class (one that is a member of a
package) to be either private or protected?
Michael Redlich - 20 Jan 2006 06:25 GMT
>      the class Test is without modifier, so , what is the type of class
> for Test? Private?
>      protected? What is the scope and behavior of class without
> modifier?

Hi hon123456:

The keywords:
  * public
  * private
  * protected
  * package

are access specifiers, not modifiers.  There is no behavior with acess
specifiers, just access.

Class Test is still of type Test (since a class is a user-defined type)
just like class ABC.  The difference, of course, is the use of the
'public' access specifier for class ABC.

In Java, when a class, method, or member variable is not preceded by an
access specifier, it defaults to 'package' access, that is all *.java
files that have a defined package name will have access to each other's
classes, methods, or member variables.  This is vastly different from
C++ where the 'package' specifier doesn't exist, and everything
defaults to 'private' access.

Hope this helps...

Sincerely,

Mike.

--- ACGNJ Java Users Group (http://www.javasig.org/)
Torkel Franzen - 20 Jan 2006 07:11 GMT
> The keywords:
>    * public
[quoted text clipped - 3 lines]
>
> are access specifiers, not modifiers.

 They are modifiers, in the terminology used in the language specification.

> Class Test is still of type Test (since a class is a user-defined type)
> just like class ABC.

 It doesn't make any obvious sense to say that the class Test is of
type Test.

> In Java, when a class, method, or member variable is not preceded by an
> access specifier, it defaults to 'package' access, that is all *.java
> files that have a defined package name will have access to each other's
> classes, methods, or member variables.

 I assume a "with default access" is implicit after "member
variables". But default access applies to types in anonymous packages
as well.
hon123456 - 20 Jan 2006 07:35 GMT
Hello Torkel

       Can u explain more detail on your last sentence  " I assume a
"with default access" is implicit after "member
variables". But default access applies to types in anonymous packages
as well. I am not quite understand. Thanks
Michael Redlich - 20 Jan 2006 15:21 GMT
>   They are modifiers, in the terminology used in the language specification.

Oops...Sorry about that!  Yes, you are correct.  These keywords (which
are access specifiers) are modifiers in declarative statements such as:

public static void main(String[] args) {}
public String getName() {}
private int number;

I just checked, and like you said, the Java specification states that
these access specifiers are used to modify the *visibility* of classes,
methods, and member variables.

I wasn't thinking about them as modifiers because I learned C++ before
Java.  The term, access specifiers, comes from C++, and I just
continued to think of public, private, protected, and package the same
way in Java.  As you may already know, in C++, these access specifiers
aren't used in front of every declaration.  So you would write
something like:

class A
   {
   private:
       int b;

   public:
       A()
           {
           }
   }

public and private are considered access declarations in the C++
specification.

>   It doesn't make any obvious sense to say that the class Test is of
> type Test.

As I mentioned, a class is a user-defined type.  Therefore, it can be
used to declare variables like a built-in type.  For example:

public class Test
   {
   //
   }

Test test = new Test();

The variable, test, is declared to be of type Test.  This is just like
saying:

int count = 0;

declares the variable, count, to be of type of type int.

Hope this helps...

Sincerely,

Mike.

--- ACGNJ Java Users Group (http://www.javasig.org/)
Torkel Franzen - 20 Jan 2006 17:54 GMT
> As I mentioned, a class is a user-defined type.

 This doesn't help make sense of the statement that the class Test
is of type Test.
Michael Redlich - 20 Jan 2006 22:22 GMT
>   This doesn't help make sense of the statement that the class Test
> is of type Test.

Torkel:

You are essentially creating your own data type by writing a class that
declares behavior and attributes.

class Fred
   {
   //
   }

class George
   {
   //
   }

Fred and George are now data types that can be used in your client code
by creating instances of them:

Fred fred = new Fred();
George george = new George();

I know for sure that the built-in types (int, float, char, etc.) have
their own constructors that would even allow you to write:

int count = new int();

I'll have to verify that in Java once I get home from work...

Hope this helps...

Sincerely,

Mike.

--- ACGNJ Java Users Group (http://www.javasig.org/)
Torkel Franzen - 21 Jan 2006 03:07 GMT
> >   This doesn't help make sense of the statement that the class Test
> > is of type Test.
>
> You are essentially creating your own data type by writing a class that
> declares behavior and attributes.

 Sure, but the class String is not of type String. Your terminology
is confused.

> I know for sure that the built-in types (int, float, char, etc.) have
> their own constructors that would even allow you to write:
>
> int count = new int();

 You seem to be thinking of some other language. This is not Java.
(Although with the introduction of autoboxing/unboxing, you can write things
like int a = new Integer(5);).
Michael Redlich - 21 Jan 2006 13:51 GMT
>   Sure, but the class String is not of type String. Your terminology
> is confused.

Hi Torkel:

OK, let me try to explain with the following examples:

public String getName()
   {
   return name;
   }

public int getCount()
   {
   return count;
   }

What's the return type in each of the methods?

public void setName(String name)
   {
   this.name = name;
   }

public void setCount(int count)
   {
   this.count = count;
   }

What is the type in the parameter list of each method?

So even though String is a class, it is a data type that can be used
just like the built-in types.  It was just defined through the class
mechanism.

> > I know for sure that the built-in types (int, float, char, etc.) have
> > their own constructors that would even allow you to write:
[quoted text clipped - 4 lines]
> (Although with the introduction of autoboxing/unboxing, you can write things
> like int a = new Integer(5);).

It looks like I had one of my mid-40s "senior moments" again.   :-)

I meant to say that this is true in C++, but needed to check if this
was true in Java.  I thought that I wrote that, but I obviously didn't.

Hope this helps...

Sincerely,

Mike.

--- ACGNJ Java Users Group (http://www.javasig.org/)
Torkel Franzen - 21 Jan 2006 13:59 GMT
> So even though String is a class, it is a data type that can be used
> just like the built-in types.  It was just defined through the class
> mechanism.

 Sure, but the class String is not of type String.
Andrew McDonagh - 21 Jan 2006 14:01 GMT
>>So even though String is a class, it is a data type that can be used
>>just like the built-in types.  It was just defined through the class
>>mechanism.
>
>   Sure, but the class String is not of type String.

what is its type then?
Roedy Green - 21 Jan 2006 14:23 GMT
On Sat, 21 Jan 2006 14:01:31 +0000, Andrew McDonagh
<news@andrewcdonagh.f2s.com> wrote, quoted or indirectly quoted
someone who said :

>>   Sure, but the class String is not of type String.
>
>what is its type then?

Maybe he is driving that String.class is of type Class.

String objects are of class String, but the String class itself is of
class Class.

String objects are sequences of chars in RAM. The String class is
pointers to bunch of machine code for instance and static methods.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Torkel Franzen - 21 Jan 2006 14:41 GMT
> what is its type then?

 To speak of the type of the class String, you must regard the class
String as itself an object. This can be done through the reflective
part of the Java language, in which reference types are represented by
objects of type Class. Thus in particular, if you want to regard the
class String as an object, it is an object - the one and only object -
of type Class<String>.
Chris Smith - 25 Jan 2006 06:22 GMT
>   To speak of the type of the class String, you must regard the class
> String as itself an object. This can be done through the reflective
> part of the Java language, in which reference types are represented by
> objects of type Class. Thus in particular, if you want to regard the
> class String as an object, it is an object - the one and only object -
> of type Class<String>.

I'd call that confusing language, though.  String.class (the object of
class Class<String>) is not the class String.  It is an object that is
used in the reflection API to represent the class String.  A class is
not an object, so it doesn't have a class.  And it's not a variable or
expression, so it doesn't have a type either.

This is an error along the same vein as the misconception that an object
of class Thread actually "is" (as opposed to "represents") a thread.  A
thread itself, of course, is an action rather than data, and can't be
contained in an object.  Same thing goes here.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Torkel Franzen - 25 Jan 2006 06:45 GMT
> I'd call that confusing language, though.  String.class (the object of
> class Class<String>) is not the class String.

 Indeed not. However, if somebody speaks of "the type of String",
this is the only apparent way of making sense of his statement.
Chris Smith - 25 Jan 2006 06:18 GMT
> >   Sure, but the class String is not of type String.
>
> what is its type then?

This is sort of a silly conversation.  The class String is not a
variable, so it doesn't have a type.  Defining the class String does
create an associated type, of course, but it's nonsense to say that
String is the type of the class itself.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Michael Redlich - 21 Jan 2006 14:43 GMT
>   Sure, but the class String is not of type String.

Torkel:

My main point is that data types can be built-in types (int, char,
float, etc.) or user-defined types by defining a class (String,
Character, Integer, etc. in the Java API or Test and ABC that you
originally defined).

Please take a moment to read Sun's definition of data types:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

I believe that this will support what I have been trying to explain.
They use the term "reference type" as opposed to a "user-defined type"
but the point is the same.

It looks like we got caught up in a lot of semantics, but as long we
all understand each other, everything is cool!

Sincerely,

Mike.

--- ACGNJ Java Users Group (http://www.javasig.org/)
Torkel Franzen - 21 Jan 2006 15:12 GMT
> It looks like we got caught up in a lot of semantics, but as long we
> all understand each other, everything is cool!

 The semantics of a programming language is an important part of
its description. See my other response, about Class<String>.
blmblm@myrealbox.com - 21 Jan 2006 15:57 GMT
>>   Sure, but the class String is not of type String.
>
[quoted text clipped - 12 lines]
>They use the term "reference type" as opposed to a "user-defined type"
>but the point is the same.

Well, depending on exactly what you think the point is.  "Reference
type" makes it clearer that non-primitive variables are references
(e.g., in C++ "string s" declares a string object, while in Java
"String s" declares a *reference to* a String object).  I mention
this just because it gets discussed so much and seems to be a source
of confusion for people coming to Java from some other languages.

"User-defined types" also might risk confusion between classes that
are part of a standard library and, hm, classes defined by a "user",
i.e., not part of a standard library.  That might be far-fetched,
though.

[ snip ]

| B. L. Massingill
| ObDisclaimer:  I don't speak for my employers; they return the favor.
Roedy Green - 20 Jan 2006 21:15 GMT
>     the class Test is without modifier, so , what is the type of class
>for Test? Private?
>     protected? What is the scope and behavior of class without
>modifier?

This is one of my pet peeves . It does not have a name.  It is
referred to with religious reverence by an indirect name "default"
which tells you where in is used. Some heretics dare name it
"package".  I suppose the orthodox might call it the "d-t" scope.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Smith - 25 Jan 2006 06:23 GMT
> This is one of my pet peeves . It does not have a name.  It is
> referred to with religious reverence by an indirect name "default"
> which tells you where in is used. Some heretics dare name it
> "package".

Hey, are you calling me a heretic?!? :)

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Ed - 25 Jan 2006 11:07 GMT
Roedy Green skrev:

> >     the class Test is without modifier, so , what is the type of class
> >for Test? Private?
[quoted text clipped - 5 lines]
> which tells you where in is used. Some heretics dare name it
> "package".  I suppose the orthodox might call it the "d-t" scope.

I hadn't realised that it doesn't have an official name.

The JLS says, "If a class or interface type is declared public, then it
may be accessed by any code, provided that the compilation unit (§7.3)
in which it is declared is observable. If a top level class or
interface type is not declared public, then it may be accessed only
from within the package in which it is declared."

So I suppose a unoffical handle for it would be just, "Not declared
public."

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Roedy Green - 25 Jan 2006 14:07 GMT
>I hadn't realised that it doesn't have an official name.

I noticed it long ago when I wrote one of the early entries in the
Java glossary on scope. I puzzled what to label the default scope in a
table of what is visible from what with the various scopes.

I also yearned to have an explicit keyword to specify the default
keyword.  when there is nothing I could not tell the difference
between "I forgot to specify a scope" and "I deliberately wanted the
default scope."
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Monique Y. Mudama - 25 Jan 2006 23:04 GMT
> I also yearned to have an explicit keyword to specify the default
> keyword.  when there is nothing I could not tell the difference
> between "I forgot to specify a scope" and "I deliberately wanted the
> default scope."

I think that "feature" is there to allow novice java programmers to
hedge their bets.  They can avoid specifying a scope until they notice
a problem ...

Okay, maybe that wasn't the intention, but that's certainly how I used
it for years.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Roedy Green - 26 Jan 2006 01:12 GMT
On Wed, 25 Jan 2006 16:04:14 -0700, "Monique Y. Mudama"
<spam@bounceswoosh.org> wrote, quoted or indirectly quoted someone who
said :

>I think that "feature" is there to allow novice java programmers to
>hedge their bets.  They can avoid specifying a scope until they notice
>a problem ...
>
>Okay, maybe that wasn't the intention, but that's certainly how I used
>it for years.

I can see having a default scope, but I would also like an way of
making it clear I deliberately thought and chose the package scope.

Also if you list things then they align better if everything has a
scope.

But even if the language stays the same, just an "anonymous" classes
have a name for discussion purposes so could the default scope.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 02 Feb 2006 22:05 GMT
>>I hadn't realised that it doesn't have an official name.
>
[quoted text clipped - 6 lines]
> between "I forgot to specify a scope" and "I deliberately wanted the
> default scope."

   This is what I do:

public class SomeClass {
 private     int privateInt;
 /*default*/ int defaultScopeInt;
 protected   int protectedInt;
 public      int publicInt;
}

   - Oliver
Hal Rosser - 20 Jan 2006 22:44 GMT
>      the class Test is without modifier, so , what is the type of class
> for Test? Private?
>      protected? What is the scope and behavior of class without
> modifier?

public, private, and protected are not 'types', so your question is in
error.
other folks have already answered your scope question.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.