Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Forums
Register
Login
Mobile
»
Android
Usage of private access modifier
chamini prashakthi
,
Ranch Hand
Apr 17, 2017 08:01:35
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
according to the following source code sum and NUMBER variables are declared as private.According to my knowledge private variables can only use within class.But following program doesn't give any error.Can some one explain the reason for this .
public class FinaleTwo { private int sum; private final int NUMBER; public FinaleTwo(int x) { NUMBER = x; } public void add(){ sum+=NUMBER; } public String toString(){ return String.format(" bjh %s", +sum); } }
public class FinaleExample { public static void main(String[] args) { FinaleTwo adding = new FinaleTwo(10); for(int i=0;i<5;i++){ adding.add(); System.out.println("value is "+adding); } } }
Stephan van Hulst
,
Saloon Keeper
staff
Apr 17, 2017 09:00:29
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
You didn't actually show us code where
sum
and
NUMBER
are being accessed outside of their declaring class' file.
chamini prashakthi
,
Ranch Hand
Apr 17, 2017 09:09:25
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
from
adding.add();
call to this add method.so, add method use those sum and NUMBER parameters. Isn't it?
Tim Moores
,
Saloon Keeper
staff
Apr 17, 2017 09:23:50
(0 cow)
(0 skulls)
1
+Pie
Number of slices to send:
Send
No, that counts as use inside of the class in which they're declared.
Post by:
autobot
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
This thread has been viewed 692 times.
Similar Threads
toString
applet with NetBeans
Private variables inherited but not accessible
Manager / worker / updated data
Adding the integers of a number
More...
All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:08:58
.