public vs private access, quick question-

  • Thread starter Thread starter z_learning_tester
  • Start date Start date
Z

z_learning_tester

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
 
z_learning_tester said:
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.
 
Great, just what I needed.
Thanks!

Jeff

Daniel O'Connell said:
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
 
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.
 
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
 
z_learning_tester said:
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.
 
Well, I'm definitely learning more than just one 'something new' every day
;-)
Thanks again!

Jeff
 
Back
Top