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

Deserializing JSON to polymorphic object model using #JsonTypeInfo & #JsonSubTypes not working?
increment NOT working in javafx controller
How to implement View Pager inside Recycler View in android in the same class?
Java. How to paint a specific cell in a JTable?
Missing java.compiler system property
How to implement/setup BPMN2 diagram with two successive exclusive gateways
How do I Implement a penDown function in my Turtle Graphics programme?
Creation of an interactive “target” in android studio
Compile JAR with manifest inside JAR?
Filter JSON for keys & values using other JSON objects
JPanel and JFrame size not changing [duplicate]
Annoymous class OnClick Listener Error
Junit test in play framework
Trying to send CSS in HTML email over Outlook with JavaMail
How to make a thread wait for a server response
JPA lazy loading and Generics

Categories

HOME
sendgrid
testing
client
urbancode
image-processing
push-notification
hp-exstream
iot
tesseract
octobercms
gis
yahoo-oauth
graphql
rubygems
ckeditor
pivotal-cloud-foundry
mouse
podio
upload
session-variables
dax
collectd
google-apps-marketplace
leiningen
seaborn
karma-jasmine
object-detection
wkwebview
wijmo
rundeck
p-value
sqlcipher
shopware
elasticsearch-ruby
facebook-apps
google-cloud-nl
mapdb
xcode-extension
web-mining
dynamics-crm-2013
ansible-playbook
user-controls
pdf-reactor
jvm-languages
auto-update
taffy
dotcover
strptime
boost-multi-index
integrity
svmlight
nstextview
feeds
sqlbulkcopy
spring-mongodb
color-profile
or-tools
visual-c++-2008
setuptools
lua-5.1
rvest
independentsoft
spidermonkey
0xdbe
event-bubbling
createprocessasuser
ami
sortedlist
graph-api-explorer
fscommand
googlemock
bitcoinj
eol
article
undefined-reference
monomac
openlaszlo
prettify
listings
jplaton
cos
shellexecute
qt-faststart
enterprisedb
hgsubversion
horizontal-accordion
libs
mtj
boost-smart-ptr
lzh
ajax-forms

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App