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.
Dheeraj said:
1 decade ago
Option 2. Is wrong? static Main () can call instance methods of class.
Penchala Naidu said:
1 decade ago
In case of Constructor concept, Static constructor cannot access non-static variables but non-static constructor can access static variables for initialization.
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);
}
}
}
Was said:
1 decade ago
Static constructor method cannot access instance method. But other static methods can access instance methods.
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?
Kamal said:
1 decade ago
Static function is accessed by static funcation only.
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.
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.
Shafi said:
1 decade ago
Static function can call the static methods and also instance 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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers