java


Check if class has member variable?


I have written a small program, in it a couple of defined classes. I am writing tests for these classes. The constructor I wrote for one of the classes has an argument that is an instance of another class. So, it looks something like this:
interface type1 {}
interface type2 {}
class class1 implements type1{
String something;
int somethingElse;
//...
}
class class2 implements type1{
String something2;
int somethingElse2;
//...
}
class class3 implements type2{
Object obj1;
Object obj2;
String blabla;
//...
class3(Object inObj1, Object inObj2, String x){
this.obj1 = inObj1;
this.obj2 = inObj2;
this.blabla = x;
}
}
As stated above, I am creating tests. There are two test classes (one for each interface). I want them to look something like this:
class ExamplesType1{
class1 type1Something1 = new class1(...);
class2 type1Something2 = new class2(...);
}
class ExamplesType2{
class3 type2Something = new class3(type1Something1, type1Something2, ...);
}
My question is, how do I access the objects that are members of ExamplesType1 from ExamplesType2 so that I may... run my tests? Assume that I cannot test both interfaces in the same class, and that I cannot define once more the objects defined in ExamplesType1, in ExamplesType2.
I know I can create a return function (for example, getType1Something()), but they are public, so shouldn't I be able to access them without defining a function?
Sorry if this is a newb question... just started Java today.
Thank you for your help! If any clarification is needed, let me know.
Assume ClassA has two instantiated classes, ClassB and ClassC as instantiated by
ClassB classB = new ClassB();
ClassC classC = new ClassC();
ClassB and ClassC public variables can then be referenced inside ClassA, for example: classB.variableName.
This can reference any object that is public from primitive types to objects.
If the classes are not instantiated, then the public variables must additionally use the static keyword to be referenced. This static reference allows you to circumvent the encapsulation from a parent class and instantiation.
In other words, ClassB can reference the parent class, ClassA to retrieve a public static instance of itself, but if it is not a static variable, it must rely on instantiation, static getter methods, or instantiation with getter methods to access those objects.
Hope this helps :)
Assume you have two classes
class ClassA {
String flag;
...
}
class ClassB {
String flag;
...
}
Them you have a few options to access field member from these classes.
The usually recommended method is to create setters and getter for the fields. This way any block holding a reference to an instance of those classes will be able to access and modify these fields.
You can also elect to make these fields public, which will also allow any block of code holding references to these classes to access the public fields.
If you want some classes to have access to those fields, but not everyone (not public) you can make them package protected. This will allow any other class within the same package to access those fields, but not anyone else. To make a field (or method) package protected, you simply do not specify an access modifier (public or private) for the field in question. You can read more here.
Lastly, and very handy for testing, is to use reflection to access the private members. Reflection is not very performant, and kind of hackish, but it can be very handy in certain situations. I have mostly used reflection to mock objects in unit tests, but have seen it used in very interesting ways as well.
Field field = MyClass.getDeclaredField("fieldName");
field.setAccessible( true );
String flag = field.get( myInstance );
PS> Writing this mostly from memory, but I believe this should get you going.
Hope this helps! Good luck!

Related Links

How to resolve java.lang.NullPointerException in scala?
Binary Data Type in H2, Sql-Server, MySql
Troubles trying to open SQLite on JAVA
Android skips lines of code
How to get DLLs, function names, and addresses from the Import Table with PortEx?
How to unpickle a file in Java?
conditional set parameter in hibernate
Where can I find org.eclipse.jface.nl_de?
Constructor is not defined in java in eclipse though it is defined [duplicate]
how do i pass varible containing json data in textview using retrofit
Is it helpful to set the variable and object to null?
Floating button setOnClickListener not gets called in Android
loadPolicyFile doesnt work in AS3
android take multiple image with camera
How to read tcpdump file in java of DARPA dataset?
What is causing this StringIndexOutofBounds exception?

Categories

HOME
google-chrome-extension
netbeans
reflection
fft
lodash
office365api
grep
read-eval-print-loop
jsp-tags
cross-browser
onelogin
adfs
modx-revolution
u-sql
messages
hex-editors
quickfix
circuit
footer
invantive-sql
immutable.js
one-hot-encoding
sox
kvc
android-widget
buildbot
qhull
underflow
apache-commons-io
poltergeist
bootstrap-duallistbox
phpfox
fopen
fifo
saas
objectanimator
bluestacks
tinymce-4
bcd
noraui
webtest
protovis
atl
wixsharp
plsql-psp
appfabric
io-redirection
file-format
multilingual
mixture-model
sqlite2
elasticsearch-plugin
greenrobot-eventbus
mesos-chronos
nomethoderror
wso2carbon
filepicker
prolog-setof
gabor-filter
carrot
flow-control
redundancy
medium.com
url-pattern
radtreelist
sts-springsourcetoolsuite
adxstudio-portals
trash
mathematica-frontend
impresspages
ubuntu-10.04
qcustomplot
qgraphicsview
make-install
apachebench
system.management
qtableview
iis-arr
muse
sframe
rake-task
qpainter
zend-route
uitouch
navigationservice
elliptic-curve
dealloc
rtmfp
algebraic-data-types
typo3-neos
terminal-services
mqx
pygit2
selected
chuck
coderush
gdata-api
wse3.0
xtype
gdlib
site.master
digest-authentication
dmx512
thunderbird-lightning
webkit.net
libs
exchange-server-2003
mtj
sector
ugc
data-acquisition

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App