Results 1 to 7 of 7

Thread: How can I access a variable

  1. #1
    Junior Member
    Join Date
    Feb 2007
    Answers
    3

    How can I access a variable

    I have a variable in a class. How can i access that variable from another class?


  2. #2
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: How can I access a variable

    Unlesss you make that variable public, you can not access that variable from another class. Other methods are there too, like make it nprotected and inhet the class.

    But if you make the variable private, you can not access the variable from outside the class. May it be another class/function or main(). That is the fundamental principal of OOPS.

    -Kalayama

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  3. #3
    Expert Member
    Join Date
    Dec 2006
    Answers
    204

    Re: How can I access a variable

    But We can access a private variable from an outside class through a public method in that class. right?


  4. #4
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: How can I access a variable

    You are right. The question is directly accessing the variable. Can you change the value of the variable from another class? You can not do that unless you have a function defined for it in this class.

    What you are actually accessing is the function of that class not the variable.

    PS: Sorry if the post sounds shabby. Scribbled it in a hurry.

    Cheers!
    Kalayama

    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

  5. #5
    Junior Member
    Join Date
    Mar 2007
    Answers
    1

    Unhappy Re: How can I access a variable

    there is an no.of ways to access it
    1.with the help of inheritance
    2.public way of declaring
    3.access with the help of objects


  6. #6
    Expert Member
    Join Date
    Dec 2006
    Answers
    204

    Re: How can I access a variable

    yeah.. No way of directly accessing it.


  7. #7
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    Re: How can I access a variable

    there is an no.of ways to access it
    1.with the help of inheritance
    2.public way of declaring
    3.access with the help of objects
    These answers naive. Find the correct things below.
    1)with the help of inheritance, you can only access protected variable (apart from public). You can not access private variable even if you inherit it.
    2)public way of declaring is not opps compiant. If the variables are declared public, one can as well declare them as strcutures. Yes. In this way you can access the class variable though.
    3)????? where do objects come from???? an object is nothing but instance of class!!! if you declare the variable as private, you can not access it with an object of the class. (of course if you have defined a function to access it, you can access the function that function and then indirectly you can access the variable. )

    sweetsage_2007, just wanted to correct you mate. The information provided by you was not complete. Hope to see some great posts from you in future. Cheers! kalayama

    Last edited by kalayama; 03-08-2007 at 08:17 AM. Reason: Formatting went to the dogs after I posted it; corrected it
    [COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

    Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact