answersLogoWhite

0


Best Answer

If you declare the main method anything other than public, it will not run.

If you declare main method as private, you would not be able to execute the class as a standalone java program. Any java class that needs to be executed as a standalone file needs to have a main method that is public, static and returns a void. Otherwise the Java compiler would not recognize the file as an executable standalone java file and would not allow you to run it.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Basically declare a class as private, i.e. private class DontTouchThis.

Note: Only inner classes can be marked as private; top level classes which are declared as private will cause a compile-time error.

class Outter{ // cannot private

// All the interesting code goes here

private class Inner{ // only this kind of classes can be private

// All the private code goes here

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Hi,

you can not declare your class as private except that is inner class.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Yes, you are free to do it, but you cannot instantiate such a class anywhere else. Only that class can create an instance of itself and no one else.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

It becomes a singleton class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you declared constructor as private?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is friend constructor?

A friend constructor is a constructor that is declared a friend of another class and that grants that constructor private access to the class in which it is declared a friend. Example: class Y { friend char* X::foo (int); // friend function friend X::X (char); // constructors can be friends friend X::~X(); // destructors can be friends }; For more information, see '11.3 Friends' in the current ISO C++ Standard.


Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


Can you define a constructor as private?

I dont think we can have Protected Constructors but yes we can have Private constructors. We can declare the constructor as Private to ensure that no other class can instantiate it. We use this in the singleton design pattern


How constructor is different from normal member function?

A constructor differs from a normal member function in three ways:A constructor never returns a result. The constructor's declaration reflects this by not even declaring the function as "void." The common design hypothesis is that a well-designed constructor cannot fail, other than maybe in an irrecoverable way (such as a fatal running out of memory).A constructor is never called explicitly except with the new operator.Constructors impose further restrictions. For example, they cannot be declared abstract or virtual, and may have visibility requirements. The common design practise is that at least the default constructor is declared public.


Why do you need a constructor as a class member?

A constructor is not a mandatory member that we need to code specifically for a class. While creating a class, even if we miss out coding the constructor, Java would create a default constructor all by itself. The constructor is usually the place where we initialize things that are required by the class. Hence it is a good practice to code the constructor for our class. Tip: If you do not want anyone to instantiate your class, you can declare the constructor as private. In that way no other class can instantiate your class.

Related questions

Can you define the constructor as private?

Yes, you can. Making a constructor private ensures that no other class can instantiate that class you just created with a private constructor. It is usually used in Singleton Patterns.


What is friend constructor?

A friend constructor is a constructor that is declared a friend of another class and that grants that constructor private access to the class in which it is declared a friend. Example: class Y { friend char* X::foo (int); // friend function friend X::X (char); // constructors can be friends friend X::~X(); // destructors can be friends }; For more information, see '11.3 Friends' in the current ISO C++ Standard.


Can you implement a constructor in private part of code?

no we cannot initialize a constructor in private in order to call a constructor from outside of a class it must be a public member.in order to create an object we should call the constructor .so only private members can implement outside of the class.


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


Is there any friend constructor in c?

C is not an object-oriented programming language so there is no friend keyword let alone friend constructors. In C++, however, constructors can be declared friends. Consider the following code where the class Y default constructor has private access to X::foo() because the Y::Y() constructor is explicitly declared a friend of class X. Note that Y must be defined before X can be defined, thus X must be forward declared. #include<iostream> class X; // fwd declaration class Y { public: Y() { X x; x.foo(); } // X::foo is private, but Y::Y() is a friend. }; class X { friend Y::Y(); // friend constructor private: void foo() {} };


Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


Is Constructor Can be Private and Protective in c?

question i understand not


When you go for private constructor?

jacks mums fit!


Can you define a constructor as private?

I dont think we can have Protected Constructors but yes we can have Private constructors. We can declare the constructor as Private to ensure that no other class can instantiate it. We use this in the singleton design pattern


How will you overload a constructor?

The same way you overload any function -- you provide a new signature for it. The signature is determined by the number and type of parameters it accepts, and whether they are const or not. Every class has a public default constructor if no other constructor is declared . Also every class has a public copy constructor whether you declare one or not. So every constructor you do declare is, in fact, an overloaded constructor. Example: class myClass { public: myClass():myInt(0){} // default ctor (no parameters) myClass(const myClass & copy){myint = copy.myInt;} // copy ctor myClass(int newInt):myInt(newInt){} // overloaded ctor private: int myint; };


How constructor is different from normal member function?

A constructor differs from a normal member function in three ways:A constructor never returns a result. The constructor's declaration reflects this by not even declaring the function as "void." The common design hypothesis is that a well-designed constructor cannot fail, other than maybe in an irrecoverable way (such as a fatal running out of memory).A constructor is never called explicitly except with the new operator.Constructors impose further restrictions. For example, they cannot be declared abstract or virtual, and may have visibility requirements. The common design practise is that at least the default constructor is declared public.