C# Programming - Constructors - Discussion

Discussion Forum : Constructors - General Questions (Q.No. 8)
8.
Can static procedures access instance data?
Yes
No
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
6 comments Page 1 of 1.

Raji said:   1 decade ago
Static procedures can only access static data.

Nishaa said:   1 decade ago
Raji ur right...static data not access instance data....

Sundar said:   1 decade ago
Instance procedures can access static data of a class because static data is common to all.

It can be accessed simply by

"ClassName.staticVarialeOrObjectName".

Example:

public class A
{

public static int countValue = 0;

public void method1()
{
int c1 = A.countValue; // accessed by the instance method of class A
}

}

public class B
{

public void method2()
{
int c2 = A.countValue; // accessed by the instance method of ofther class B
}

}

Hope this will help you. Have a nice day!

Pradip said:   1 decade ago
Static data not access instance data.

But instance procedure can access the static data.

Jaya said:   10 years ago
I think answer is A because:

class Program
{
public int calculation(int x,int y)
{
int val=x*y;
return val;
}
static void Main(String[] args)
{
Program p=new Program();
int newvalue=p.calculation(23,12);
Console.WriteLine(newvalue);
Console.Readkey();
}
}


Here in above program I define a non-static method, inside this method I define one parameterized constructor.

In that same method I define static method and create object of Program class and through object p, I called calculation method.

Naviddya said:   7 years ago
Static is class level so it can't instance the objects.

Static is a shared one, it can be accessed by anyone.

Post your comments here:

Your comments will be displayed after verification.