B is NOT correct since a class with a private constructor can be instantiated through a static member function of the class. Try out the following code:
B is NOT correct since a class with a private constructor can be instantiated through a static member function of the class. Try out the following code:
Call PrivateConstructor.CreateObj() from Main(). It outputs "Instantiating private constructor"
anand
Jan 30th, 2006
A and B Both
class
Class1
{
/// <summary>/// The main entry point for the application./// </summary>
[STAThread]
static void Main(string[] args)
{
//// TODO: Add code to start application here//
}
private Class1()
{
}
}
public class class2
{
public class2()
{
Class1 cl =
new Class1();
}
}
Error Got is:-
class1:class1() is inaccessible due to protection level
s
Feb 15th, 2006
only A ics correct because jus saying tht a class has a private constructor does not mean that it cant have any static methods which can be used to instantiate the classs. the option B is incomplete to be called true.
when you are trying to instanciate the class which has private constructor , you will get the follwoing error.
ConsoleApplication1.PrivateConst.PrivateConst()' is inaccessible due to its protection level
Yazdani
Apr 15th, 2006
This is reply to Sameekha (She is right)
Answer is : A
If we are declaring a Constructor as Private we cannot instatiate it Directly.
Here all the methods should be declared as static so that you can instantiate and call a methods directly
ClassName.StaticMethodName();
Currect me if i am wrong
thanks
sean
Apr 20th, 2006
Ans: A&B
You can call static method does not means the a class are instantiable.
Static memebers are part of class, while instnace memeber are associated with the instances of a type. In C#, it is not legal to access a static method or member variable through instance. By example
class
TestStatic{static int num;
}
TestStatic ts =
new TestStatic();
Console.WriteLine(ts.num);
'Exercise.TestStatic.num' is inaccessible due to its protection level
public static PrivateConstructor CreateObj() { PrivateConstructor p = new PrivateConstructor(); return p; } public void message() { Console.WriteLine("Calling non static member.."); }
} class createObjClass { public static void Main() { PrivateConstructor pc = PrivateConstructor.CreateObj(); pc.message(); } }
Only Choice is A correct as if we declare constructor private still we can access that constructor using some member function of the class. Object can still be created.
Guest
Jul 16th, 2006
we can prevent the class from interting by using keyword sealed
They only contain static members.
They cannot be instantiated.
They are sealed.
They cannot contain Instance Constructors
a static class is that the compiler can check to make sure that no instance members are accidentally added.
The compiler will guarantee that instances of this class cannot be created.
Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor
Pankaj
May 6th, 2014
b. By making constructor private,we can not instantiate the class.
Simply declare the class using the abstract keyword. As oppose to a regular (concrete class), abstract classes are not instantiated but are used to derived other subclasses.
For B, we cannot instantiate the class if there is only one constructor and that only constructor is private, otherwise, if there is at least one constructor is public, then that class is instantiatable.
For C, keyword "seal" only means this class cannot have sub-classes, but we can still instantiate it.
I did like to add another answer to this question: Static class cant be instantiable too
ramya
Oct 13th, 2015
Making class as Abstract
Naveen Kumar Shivanadri
Sep 8th, 2016
Answer is A is always correct and B is also correct only the situation when the class does not have the static methods on it (in the question, there is no mentioned that there is no static method in it)
So, only A is correct answer.
How do you make a class not instantiable
B) Having a Private constructor
C) Making the class sealed
D) Both A & B
Related Answered Questions
Related Open Questions