By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,980 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

public vs private access, quick question-

P: n/a
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

int myVal = class1.method();

It seems that you should not be able to, after all from class2, you should
not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"z_learning_tester" <so*****@microsoft.com> wrote in message
news:RIPBc.88157$HG.75569@attbi_s53...
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

There are no private classes(atleast, not at top level, nested classes can
be private). For non-nested classes(whats the term one would use here,
anyway?) there are only two accessbilities: public and internal. A class
without a specifier is internal and accessible to every other class in that
assembly.
int myVal = class1.method();

It seems that you should not be able to, after all from class2, you should
not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff

Nov 16 '05 #2

P: n/a
Great, just what I needed.
Thanks!

Jeff

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:uN*************@TK2MSFTNGP11.phx.gbl...

"z_learning_tester" <so*****@microsoft.com> wrote in message
news:RIPBc.88157$HG.75569@attbi_s53...
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

There are no private classes(atleast, not at top level, nested classes can
be private). For non-nested classes(whats the term one would use here,
anyway?) there are only two accessbilities: public and internal. A class
without a specifier is internal and accessible to every other class in

that assembly.
int myVal = class1.method();

It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff


Nov 16 '05 #3

P: n/a
Jeff,

Remember that private classes can only be declared INSIDE another
class. The private class member would only be accessible by the class
that it is inside, and in turn, the static public method as well. Here
is a quick example:

public class MainClass
{
public MainClass()
{
// you can call the static public method from here
MyPrivateClass.TestMethod();
}

// private classes can only be declared inside another class
private class MyPrivateClass
{
public MyPrivateClass()
{
// it works here as well of course
TestMethod();
}

public static void TestMethod()
{
}
}
}

public class AnotherClass
{
public AnotherClass()
{
// it doesn't work here, MyPrivateClass isn't accessible
MainClass.MyPrivateClass.TestMethod();
}
}

Hope that clears things up.

"z_learning_tester" <so*****@microsoft.com> wrote in message news:<RIPBc.88157$HG.75569@attbi_s53>...
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

int myVal = class1.method();

It seems that you should not be able to, after all from class2, you should
not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff

Nov 16 '05 #4

P: n/a
Thanks for the response.
Your example explained my question perfectly :-)

But your example begs another question-
You say "private class MyPrivateClass" then use a public constructor "public
MyPrivateClass()"
Is that right?
Sorry I still get confused on the basics sometimes. I think constructors are
always public even if their class is private, though it seems like a
contradiction...

Oops, then another question- you are calling the method "TestMethod()" from
inside the constructor.
Can you do that?
All I have seen so far inside constructors is variable initializations.
Again, my book is bad. Very bad.

Thanks!

Jeff

"Eli Block" <eb****@gmail.com> wrote in message
news:76*************************@posting.google.co m...
Jeff,

Remember that private classes can only be declared INSIDE another
class. The private class member would only be accessible by the class
that it is inside, and in turn, the static public method as well. Here
is a quick example:

public class MainClass
{
public MainClass()
{
// you can call the static public method from here
MyPrivateClass.TestMethod();
}

// private classes can only be declared inside another class
private class MyPrivateClass
{
public MyPrivateClass()
{
// it works here as well of course
TestMethod();
}

public static void TestMethod()
{
}
}
}

public class AnotherClass
{
public AnotherClass()
{
// it doesn't work here, MyPrivateClass isn't accessible
MainClass.MyPrivateClass.TestMethod();
}
}

Hope that clears things up.

"z_learning_tester" <so*****@microsoft.com> wrote in message

news:<RIPBc.88157$HG.75569@attbi_s53>...
Quick question-
What happens if you have a private class with a public static method?
Can you still say the following?
Lets say you are making this call from another class, say class2...

int myVal = class1.method();

It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is
effectively wasted.

Please correct me if I'm wrong.
Thanks,

Jeff

Nov 16 '05 #5

P: n/a
z_learning_tester <so*****@microsoft.com> wrote:
Thanks for the response.
Your example explained my question perfectly :-)

But your example begs another question-
You say "private class MyPrivateClass" then use a public constructor "public
MyPrivateClass()"
Is that right?
Sorry I still get confused on the basics sometimes. I think constructors are
always public even if their class is private, though it seems like a
contradiction...
No, constructors aren't always public. You can have private
constructors which would only be able to be called within the class. If
a private class has a public (or internal) constructor, then the
enclosing class will be able to call it.
Oops, then another question- you are calling the method "TestMethod()" from
inside the constructor.
Can you do that?


You certainly can. It's generally a bad idea to call *virtual* methods
inside constructors though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
Well, I'm definitely learning more than just one 'something new' every day
;-)
Thanks again!

Jeff
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
z_learning_tester <so*****@microsoft.com> wrote:
Thanks for the response.
Your example explained my question perfectly :-)

But your example begs another question-
You say "private class MyPrivateClass" then use a public constructor "public MyPrivateClass()"
Is that right?
Sorry I still get confused on the basics sometimes. I think constructors are always public even if their class is private, though it seems like a
contradiction...


No, constructors aren't always public. You can have private
constructors which would only be able to be called within the class. If
a private class has a public (or internal) constructor, then the
enclosing class will be able to call it.
Oops, then another question- you are calling the method "TestMethod()" from inside the constructor.
Can you do that?


You certainly can. It's generally a bad idea to call *virtual* methods
inside constructors though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.