473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

public vs private access, quick question-

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
6 3560

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
10
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
27
by: thomasp | last post by:
Variables that I would like to make available to all forms and modules in my program, where should I declare them? At the momment I just created a module and have them all declared public there. ...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
2
by: Sky | last post by:
Hello: I'm trying to make sense of snk files, when to use, under what conditions to regenerate new ones,...can someone take a look if these statemes make sense? And then the final questions at the...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
6
by: Ajay Martin | last post by:
Why would it be reasonable for someone to argue that it is incorrect to allow a public member inherited from a public base class to be redefined as private?
23
by: Chris Gordon-Smith | last post by:
Hello All I have a base class called Action_Request, and a set of classes corresponding to different kinds of Action_Request, each of which inherits from Action_Request. Eg:- class ...
2
by: fgh.vbn.rty | last post by:
Hi, I'm not sure if i'm asking the question correctly but anyway here it is. Say I have 3 classes - class A, class B, class R. 1) A and B are the building blocks and R is like a repository...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.