C# Programming - Constructors - Discussion
Discussion Forum : Constructors - General Questions (Q.No. 4)
4.
Which of the following statements are correct about static functions?
- Static functions can access only static data.
- Static functions cannot call instance functions.
- It is necessary to initialize static data.
- Instance functions can call static functions and access static data.
- this reference is passed to static functions.
Discussion:
12 comments Page 1 of 2.
Deepak said:
9 years ago
Please. Don't mention method as function. Method is a method. Try to make improve terminology.
Pravin Magar said:
9 years ago
Yes, you are correct @Jaya.
Static functions can have both methods.
Static functions can have both methods.
Jaya said:
10 years ago
Answer is A.
Static functions can have both static methods and also non-static methods.
Static functions can have both static methods and also non-static methods.
Shafi said:
1 decade ago
Static function can call the static methods and also instance methods.
Sank said:
1 decade ago
Static members can be accessed by static and non-static methods.
Non-static members can not be accessed by static methods.
It only access static members.
Non-static members can not be accessed by static methods.
It only access static members.
Ganesh said:
1 decade ago
If You declare any method as static then the method only access static function and static variable because of static function can by using Class name without creating any instance of that class and non-static member always call by using instance of class.
For that if static function contain any non-static member there is problem occur.
For that if static function contain any non-static member there is problem occur.
Kamal said:
1 decade ago
Static function is accessed by static funcation only.
Bhavin Umaraniya said:
1 decade ago
Dear Shiridish, what you done is that, you instantiated class test itself within static function. You can not access any instance method or variable declared at class level, that means instance method and variables are not accessible to static methods. Because static method and variable assigned with single time memory shared by all instance of that class, then how it decide which instance it belongs to?
Was said:
1 decade ago
Static constructor method cannot access instance method. But other static methods can access instance methods.
Shiridish said:
1 decade ago
2. is wrong. static functions can call instance functions.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
test t = new test();
int res= t.add(2, 3);
Console.WriteLine(res);
Console.Read();
}
}
class test
{
public int add(int a, int b)
{
int c = a + b;
return (c);
}
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
test t = new test();
int res= t.add(2, 3);
Console.WriteLine(res);
Console.Read();
}
}
class test
{
public int add(int a, int b)
{
int c = a + b;
return (c);
}
}
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers