Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java C#4.0 OOP
I have created a class as follows.

 
       package basics;
 

        public class Basics {
    
          public int intMaths;
          public int intEnglish;
          public int intScience;
    
          }
 
   
 
Then I have created another class as the following and both classes are in the same package.
 
 
          package basics;
 
           public class AccessVaria {
    
    
    
           }
 
   
 
Now I want to access the variable in the Basics.java class from the AccessVaria.java class.
 
I have tried to do that like this
 
 
     intMaths = 80;
 
   
 
But I cannot do that and give me a suggestion. Do java always request for getter() & setter() methods to access variables from different classes even if the variables have the public access modifier ?
 
I have access public variables in one class from another class in C#.Net but they didn't give suchlike bad experience.
 
So if someone can explain how to solve this matter ?
 
Thank You!
Chiranthaka
Posted 17-Jun-13 2:35am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In Java, to access any properties of a given class, you have to create the object first.
Therefore if you want to access the properties of class Basics inside class AccessVaria , then you have to create a instance of class Basics inside AccessVaria.
For more information click here.
 
"..Do java always request for getter() & setter() methods to access variables.."
 
Java suggest you not to access the properties directly, use getter/setter method to access the properties. It is a coding convention.
  Permalink  
Comments
CPallini at 17-Jun-13 8:42am
   
I would say: "In Java, to access any, but static, properties of a given class, you have to create the object first"
-- Carlo the Nitpick
Shubhashish_Mandal at 17-Jun-13 8:51am
   
yes I agree.
Chiranthaka Sampath at 17-Jun-13 9:39am
   
Thanks for your comment !
Chiranthaka Sampath at 17-Jun-13 13:34pm
   
I have created an object in the AccessVaria class but that object cannot be use like this
 
private Basics oBasics = new Basics();
 
oBasics.intMaths=10;
 
But when I used that object declared inside the main() it can be easily use.
 
Then what is the problem ?
sorawit amorn at 20-Jun-13 15:46pm
   
you CAN initialize Basics object that way in AccessVaria. It would look like
public class AccessVaria{
private Basic bs = new Basic();

public void DoSomething(){
bs.intMaths = 10;
}
}
 
However, since bs object is private, you will not be able to use this instance in any other classes.
 
Please be sure to read the coding convention, this will help you a lot later on
Shubhashish_Mandal at 18-Jun-13 2:59am
   
post your code. did you create and set the instance value inside any method ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 17 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100