C# Programming - Constructors - Discussion
Discussion Forum : Constructors - General Questions (Q.No. 8)
8.
Can static procedures access instance data?
Discussion:
6 comments Page 1 of 1.
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.
Static is a shared one, it can be accessed by anyone.
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.
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.
Pradip said:
1 decade ago
Static data not access instance data.
But instance procedure can access the static data.
But instance procedure can access the static 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!
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!
Nishaa said:
1 decade ago
Raji ur right...static data not access instance data....
Raji said:
1 decade ago
Static procedures can only access static data.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers