Generic Getter and Setters

Hi all,
I want to dynamically access getter and setter methods depending on the variables i have
suppose i have a variable
String checkGetter;
and a getter method
public String getCheckGetter(){return this.checkGetter;}
and in addition to that some more variables and their getters and setters
and i want to access all the getter/setter methods dynamically ,if i am having only the variable name(as a String). I dont want to add code for accessing these methods, instead put in a loop and do my work.
Can one achieve this in Java.
Thanks in advance.
Regards.

neo_java wrote:
I am just trying to understand if there is any way to assign values to variables where the value to be assigned to the variable depends on run time
(here the variable is important to me which changes dynamically).Rene's first Law of Anti-Static: every idea including the word "dynamically" can be safely disregarded as foolish.
You can not assign anything to variables by names. You can assign values to instance or class attributes using Reflection. You can also drive a car backwards all the time. It's awkward, it won't work nicely and it can crash quite quickly. You could also use a map where you store a value assigned to a certain key or name, as already mentioned. Or you could pass the name (why do you have the name in the first place!? No good.) along with the value to the instance and let the instance find out what to do by itself.

Similar Messages

  • Why do we need get and set methods?

    It is considered good design practice to keep all class data private and
    provide get and set methods for accessing the data in a controlled
    manner.
    So, instead of directly accessing the class data, you use getter and setters.
    I do not really feel the need to use get and set methods.
    How does that achieve encapsulation when ultimately the class data is being exposed?

    A couple of reasons why to use get and set:
    1. For example you can set an int value for month, if a user sets this to somting
    higher than 12 (or 11 if it's zero based) you want to handle that by either
    throwing an exception or adding a year for every time it can be devided by 12.
    If you dont do it in set you'll have a whole bunch of methods that might need to
    correct the value before retreiving the eventual date.
    2. If for some reason you have security set up for certain values you can
    implement this in the get method. When this is done somewhere else you have
    a whole bunch of methods retreiving the info in other classes that need to check
    first. (a canGet method could allso be used). Another good reason to use get
    is when the information needs to be converted depending on the consumer
    calling the get method.
    3. If any logic in 1 or 2 changes you'll have a bunch of code to change.
    If you feel there is no need to implement any security, error handling (on
    compiling because get and set might throw something) or validation when
    setting/getting these values there is still the argument of readabillity.
    Eclipse has a feature that will generate getters and setters for you so it's not
    like there is a lot of extra typing involved.
    Got interupted whyle typig so sorry to repeat any answer given before.

  • 'ResourceDictionary' root element is a generic type and requires a x:Class attribute to support the x:TypeArguments attribute sp

    Error : 'ResourceDictionary' root element is a generic type and requires a x:Class attribute to support the x:TypeArguments attribute specified on the root element tag.
    Hi,
    I get this error when i include some namespaces in my ResourceDictionary to specify a Style for a custom control.
    Can anyone help me?
    Thx
    Stardusty

    Hi,
    That's the whole point. I don't want to use x:TypeArguments on a ResourceDictionary but the compiler says it needs it.
    And i don't know why.
    This code give no error:
    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespaceystem;assembly=mscorlib">  
    </ResourceDictionary>
    And by adding 3 namespaces it gives that weard error:
    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:MyTime.View.Controls"
    xmlns:converters="clr-namespace:MyTime.View.Converters"
    xmlns:validationrules="clr-namespace:MyTime.View.ValidationRules"
    xmlns:sys="clr-namespaceystem;assembly=mscorlib">  
    </ResourceDictionary>

  • Big big problems using Generics, BeanInfo and Introspector

    I'm having a big big problem with Introspector class. Let me setup the problem:
    public interface VO<T, V> {
      public T getOid();
      public void setOid(T oid);
      public V getVersion();
      public void setVersion(V version);
    public interface CarVO extends VO<Long, Integer> {
      public String getPlate();
      public void setPlate(String plate);
    public class CarVOImpl implements CarVO {
      private Long oid;
      private Integer version;
      private String plate;
      public Long getOid() { return this.oid; }
      public void setOid(Long oid) { this.oid = oid; }
      public String getPlate() { return this.plate; }
      public void setPlate(String plate) { this.plate = plate; }
      public Integer getVersion() { return this.version; }
      public void setVersion(Integer version) { this.version = version; }
    public class Cool {
      public static void main(String[] args) {
        try {
          CarVO vo = new CarVOImpl();
          PropertyDescriptor[] properties = Introspector.getBeanInfo(vo.getClass()).getPropertyDescriptors();
          for (int i = 0; i < properties.length; i++) {
            System.out.println("Name " + properties.getName());
    System.out.println("PropertyType " + properties[i].getPropertyType());
    } catch (Exception e) { e.printStackTrace(); }
    Well. This code is 100% alright, compilation OK. In runtime, no matter what I do, inverting the generic types, modifying interfaces, whatever, the property with the first generic type defined (in this example, oid) will be wrong resolved to java.lang.Object, instead the correct java.lang.Long.
    Name oid
    PropertyType class java.lang.ObjectAltering the order of the getter and setter in the source code seems to affect the result of Introspector!!! (ahhhhhhh)
    This is probably a damm bug of Introspector class, that is private of Sun. Any idea to correct this problem?

    Hi,
    JSF 1.1 doesn't coexist nicely with HTML components.
    So if your goal is to build layouts in HTML then this
    will not produce the result you want
    Skins in 10.1.3 are slightly different than in 11
    (selectors may have changed their names). If you need
    to specify the class property to make skins work then
    this indicates that the skin selectors you use are
    not the correct ones.
    FrankHi Frank,
    thank you for your help, I'm replacing every html component with adf one, panelGroup, panelHorizontal, panelBorder, objectImage etc... etc... but I have a problem...
    I need to put an object component about Macromedia Flash and I cannot find any adf component to replace it. Any idea please?
    Thank you again.
    Ciao.
    Ricky

  • EJB newbie: Why getters and setters when I don't use them?

    I'd like to know why I have to write getters and setters in my bean implementaion class when I don't use them. I have SQL statements in my entity bean class that does querying and updates and I don't need to get or set individual fields of my databse.
    This must be a very trivial question for many, but I really dont know the answer to this one.
    Thanks.

    You can't get rid of them because they are built into iOS. It's annoying I know.

  • Question about getters and setters

    I want to ask you a bit simple question. I know, you can show me google but i know to understand this by my example. My program code:
    class Readers {
         int age;
    public Readers(){
         age = 45;
    public int getAge() {
         return age;
    public void setAge(int age) {
         this.age = age;
    public void print_age(){
         System.out.println(+age);
    class Main {
         public static void main(String[] Args) {
         Readers john = new Readers();
         john.print_age();
    Why do i need use getters and setters? Because without these methods I get the same result. My program works correctly. Can you explain the benefit of using getters and setters?
    Thanks in advance ;)

    First, nobody said that you should use getters and setters in the first place. The only thing that's worse than dumb mutators/accessors would be the exposure of the attribute itself.
    Second, you certainly don't need mutators "internally".
    Third: consider you redesigning your class to store a person's birthdate instead of its age - makes sense if one thinks about it, doesn't it? If you have coded your entire program to access the "age" attribute of the object, you know have a problem because it was replaced by "birthday". The getter at least could calculate the current age from the birthdate - so no rewriting of the rest of your program would be necessary, just a small change in your getter.

  • Project Lombok - easier getters and setters

    I came across [Project Lombok|http://projectlombok.org/] recently and I think it greatly simplifies common Java patterns. All those getters and setters needed to satisfy JPA, JSF, and CDI make conceptually trivial classes become hundred-line monsters. If you need to change a type (e.g. int to long) or a name, then you need to do the same thing three or more times, or you can delete and generate everything again, but this can be risky: you might delete and regenerate a method that contained some little but necessary extra logic (a null check with ?:, for example), which could be indistinguishable from boilerplate code at first sight.
    I know this will be met with resistance, but I think a Lombok-like project should be incorporated into Java SE 8 or 9. Look at the great productivity improvements we had with Java 5 and Java 7 (specifically Project Coin). Look how easy it is to create an EJB in Java EE 6. People that would never consider EJBs in the past are now using them (me included) and enjoying!
    Maybe Java tools could at least provide official tools to support Lombok, because it currently uses unsupported interfaces to do its work.
    I think this post, if taken as an official feature request, would be off-topic here, but hey, I already have an account here and the conversation must start somewhere :-)

    MarcusF. wrote:
    All those getters and setters needed to satisfy JPA, JSF, and CDI make conceptually trivial classes become hundred-line monsters. Only when you're doing something very wrong design-wise. Getters and setters are no problem, you generate them anyway. But go ahead, keep believing the incredibly trivial is what is keeping you from being productive.
    Look how easy it is to create an EJB in Java EE 6.Yes its easier to CREATE an EJB. But it is still as hard as ever to understand the nuances of the technology and properly apply it without cutting into your own fingers. You still need to properly understand transaction management for example. But also stateless design, remote invocation, messaging, concurrency, etc. etc. Its still very, very hard. But yeah, annotations in stead of XML crud. That made -all- the difference right?
    It isn't code that makes things easier. It never was and it never will be. But people keep focusing on simplifying the actual programming by spitting out API after API and framework after framework and in the mean time the technology never gets any easier, or better. Because the code is not where the problem is.
    IMO of course.

  • How struts invoking form getters and setters of a property

    hi,
    can anyone explain me how struts framework was calling the form properties getters and setters
    i have an idea like preparing a string such that
    if propert of text box is propertName, preparring a string with get and set making first letter capital
    setPropertyName
    getPropertyName
    and invoking that method in form property
    but i dont think this is the exact way struts follow , there could be a prcise and direct way to invoke propert methods
    i have another idea
    getting the declared method of the form class and search for the string propertyname in the methods name, and sure this is also not perfect way
    with regards
    satya

    can anyone tell where i can find libraries for access/manipulate beans.
    and sample example to invoke getters and setter of the property but not like preparing String "setPropertyName" or "getPropertyName" and invoking the method i beleive there should be standard way
    and also setPropertyName may take different type parameters i cannot keep number of if conditions to check for different datatypes
    with regards
    satya

  • Getters and Setters in ActionScript and ECMAScript

    Hi ,
    This line is taken from a book , here the Author mentions that
    In ActionScript , getters and setters is not necessary , they are a part of of the core ECMAScript language.
    Can anybody please let me know what is meant by ECMAScript language ??
    Thanks in advance .

    ECMAScript is an ECMA standard.
    Getters and setters are not mandatory (that's what they mean).
    However, they can be useful in many cases.
    - You can check the value in a setter before assigning (e.g checking if it's null)
    - You cannot override properties of a superclass but you can getters/setters just as you can methods
    - You can take some action when the value of the property is changed. There are many examples in UIComponent classes of the Flex framework.
    - You can put a breakpoint in a setter in order to debug data binding (see if it triggers)
    - In order to optimize data binding, you can write [Bindable event="YourCustomEventNameHere")] before the getter/setter and dispatch this event when the value is set so as to enhance performance of data binding (if you just write [Bindable], your properties will be processed by the framework even if their value does not change).
    Also, note how the synthax is different than Java.
    // ActionScript
    private var _myProperty:uint;
    public function get myProperty():uint
      return _myProperty;
    public function set myProperty(value:uint):void
      _myProperty = value;
    // Java
    private Integer myProperty;
    public Integer getMyProperty():
      return myProperty;
    public void setMyProperty(value:Integer):
      myProperty = value;
    Avoid creating getters/setters a la Java in ActionScript (i.e not using the get/set keywords). This is something I have seen in some implementations or auto-generated code from UML.

  • ValueRef'ing array elements, getters and setters

    Hi,
    I was wondering what the appropriate way to have the getters and setters for an array to be in the Model object.
    For example, what should the getter/setter for stuff in Stuff.java be?
    In the jsp page:
    <input_text .... valueRef="Stuff.stuff[0].property">
    In Stuff.java:
    private Blah[] stuff = new Blah[25];
    In Blah.java
    private String property;

    At a minimum,
    public Blah[] getStuff() { ... };Array accessors are always dangerous in Java, since they're necessarily mutable; if you don't want mutating, you have to clone the array every time you return it. Lists are preferable in this regard. OTOH, Lists aren't at all typesafe.
    -- Adam Winer

  • Do you need getters and setters for C types?

    Say, I have class declaration like so:
    @interface MyClass : NSObject
    int nValue;
    -(void)someMethod:(int)v;
    @end
    and then the implementation:
    #import "MyClass.h"
    @implementation MyClass
    -(id)init
    //Initialization
    if(self = [super init])
    nValue = 0;
    return self;
    -(void)dealloc
    [super dealloc];
    -(void)someMethod:(int)v
    int r = nValue;
    nValue += r + v;
    @end
    Do I need a getter and a setter for the 'nValue' variable?

    Den B. wrote:
    Do I need a getter and a setter for the 'nValue' variable?
    No more so than you would in C++.
    In Objective-C, if you really need to provide access to an objects internal values, you should you properties. If you don't need properties, then getters and setters are just as bad as they are anywhere.

  • Duplicate getters and setters

    Hi,
    Can any one tell me why do we need getters and setters both in class and backing bean? Is not a duplication?
    Here is an example
    In my class Dept I have getDept_Code,getDept_Name and setDept_Code,set_DeptName.
    and in my backing bean(DeptBean) I have same getters and setters.
    please note that in my Dept.jsp I use these as follows
    value="#{DeptBean.dept_code}".
    value="#{DeptBean.dept_name}"
    My question is why dont we use getters and setters in Dept class. and do we need to have getters and setters in backing bean?
    plz explain.

    You just can use the getters in Dept? You don't need to "flatten out the objects".
    MyDto:public class MyDto {
       private String field1;
       private String field2;
       // + getters + setters
    }MyBean:public class MyBean {
        private MyDto myDto;
        // + getter + setter
    }JSF<h:outputText value="#{myBean.myDto.field1}" />

  • Difference between Generic extraction and Generic Delta

    Hi Experts,
    Pleasee give of some information below  query:
    1) Difference between Generic extraction and Generic Delta.
    2) How to achieve Generic delta.
    3) When we go for Generic delta instead of generic extraction.
    Advance ThankU. Points vl be assigned.
    Thanks,
    Ragu.R

    Hi,
    Generic delta is the delta load done using Generic DS.
    Generic extraction
    Usage:
    1. When the standard extractors are not supporting the extraction what you need. If SAP does not have a standard extractor for your need to get data from R3, you would have to go for generic extractor.
    2. If you create a custom object say by combining certain base tables in R3 say custom tables ZTAB1 and ZTAB2. These two tables are not SAP provided tables and there will not be any standard extractors. So cases like this you will have to go for generic extractors.
    How:
    You have to use RSO2 transaction and you can also set delta based on, one of the three characteristics such as timestamp, calday or pointer (a sequence no).
    once you create it and activate it. The extractor will be available in ROOSOURCE - (table in R3 where all the data sources are available).
    Refer:
    /people/siegfried.szameitat/blog/2005/09/29/generic-extraction-via-function-module
    http://help.sap.com/saphelp_nw04/helpdata/en/3f/548c9ec754ee4d90188a4f108e0121/content.htm
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/84bf4d68-0601-0010-13b5-b062adbb3e33
    Generic Extraction via Function Module
    /people/siegfried.szameitat/blog/2005/09/29/generic-extraction-via-function-module
    thanks,
    JituK

  • My new purchases will not sync into my playlist and get and will not show up on my iphone playlist of the same name, and I just tried to sync a new cd into my library and it is not showing up on my iphone.

    My new purchases will not sync into my playlist and get and will not show up on my iphone playlist of the same name, and I just tried to sync a new cd into my library and it is not showing up on my iphone.

    Hello there, Capricious13.
    There are great pointers in the following Knowledge Base article. Depending on where you see the content and where you want to get it to, this should guide you to putting it in the right place:
    Downloading past purchases from the App Store, iBookstore, and iTunes Store
    http://support.apple.com/kb/ht2519
    Thanks for reaching out to Apple Support Communities.
    Cheers,
    Pedro D.

  • Get and Put options are greyed out in DW CS4

    I am a fairly green Dreamweaver user, and recently (finally) upgraded from MX4 to CS4.  I am trying to get a file from my local c:drive and both the get and put functions are greyed out.  Any idea how to enable these functions?  I do not have the file that I wish to transfer open.

    When you say you are trying to get a file from your local c:drive, dio you mean that you are trying to import a file into your site from elsewhere on your computer, rather than 'getting' it from your remote server.  If you are trying to import a file from your local c:drive don't use the 'get' or 'put' buttons because they are just for remote connections, instead just use windows explorer to locate the file then either copy or cut and paste the file into the folder for your site.
    Richard

Maybe you are looking for

  • Iphone 4s calendar alert issue

    I tend to put 2 alerts on my calendar events, one for 1 day before and the second one for 1 hour before (or some other on the day of event).  I have noticed that when I put in the first alert for 1 day before and then enter the second alert for 1 hou

  • XSLT 2.0 supported in Oracle BPEL ?

    Hi, is XSLT 2.0 (schema-aware) supported or is there any way to change the built-in XSLT parser to Saxon or another one? Thanks.

  • How to read a server side file in Applet?

    When I used ZipFile like this, ZipFile zf = new ZipFile("http://xxx.com/res.jar"); It throwed exception about access denied, File pemission... How to read a server side file in Applet?

  • How to display a button with an icon?

    Hi, I'd like to display an SAP-icon on a button. In this thread How can I show an Icon in the tapStripItem-title? Thomas Jung gave the following example: > I don't know if this definetely works with tabstrips > items but it does work with buttons and

  • My Iphone contacts dis appearing

    I have (1) ipad mini & (2)iphone 5, and i use (3) Google for e-mail & contact & calendar. I have made a setup, so that when ever update or new entry on one of those, will update to the others, And I installed Apps name REMOVE DUPLICATE CONTACT by zho