Dynamic variable declaration in java program

declaring a variable with the name i.e. contained as a value of other variable.
say String varname = new String("Name");
now how to declare a variable called Name with some datatype........using varname.............
hope it is not very confusing........

You can't do it. There should be no reason to do it either.
If you want to store values against particular names, use a Map e.g. HashMap.

Similar Messages

  • Get all the variables declaration in a program

    Is there a possible way to get all the variables declarations ( name and type ) inside a java program and not only the fields that you can easily get with reflection mechanism but also local variables inside a method?

    Kayaman wrote:
    Jigsaw23 wrote:
    local variables inside a method?Nope, you'd have to get inside the call stack for that and there's no easy mechanism for that.Even that wouldn't do it, since not all code is on the call stack at all times. There are ways to do it, using bytecode engineering, but I'm not going to get into that because if you don't already know how to do it, I doubt you have a problem that genuinely requires it.
    It's a bogus requirement anyway. Whatever you're trying to do, OP, it's doomed to failure. What are you trying to do? I mean, what were you doing that led to you thinking "If I knew what those local variable were, I'd manage it!"?

  • How to access variables declared in java class file from jsp

    i have a java package which reads values from property file. i have imported the package.classname in jsp file and also i have created an object for the class file like
    classname object=new classname();
    now iam able to access only the methods defined in the class but not the variables. i have defined connection properties in class file.
    in jsp i need to use
    statement=con.createstatement(); but it shows variable not declared.
    con is declared in java class file.
    how to access the variables?
    thanks

    here is the code
    * testbean.java
    * Created on October 31, 2006, 12:14 PM
    package property;
    import java.beans.*;
    import java.io.Serializable;
    public class testbean extends Object implements Serializable {
    public String sampleProperty="test2";
        public String getSampleProperty() {
            return sampleProperty;
    }jsp file
    <%@ page contentType="text/html;charset=windows-1252"%>
    <%@ page import="java.sql.*,java.util.*"%>
    <html>
    <head>
    <title>Schedule Details</title>
    </head>
    <jsp:useBean id="ConProp" class="property.testbean"/>
    <body>
    Messge is : <jsp:getProperty name="msg" property="sampleProperty"/>
    <%
      out.println(ConProp.sampleProperty);
    %>
    </body>
    </html>out.println(ConProp.sampleProperty) prints null value.
    is this the right procedure to access bean variables
    thanks

  • Trace a variable in a java program

    Hi all,
    I'm doing a project which require me to trace a variable or method and record down their occurrence and modification log in a java program. Are there any tools available?
    Thx

    I think java profiler can do the job. There are sharewares i know of like JSprint and JProfiler.

  • Dynamic Variable Declare

    Hi experts,
    How can i declare Dynamic variable under loop,
    I have Data from CABN Table with Char Name /Data type/Length.
    Is there any function module or method where i provide the parameters for declaration.
    thanks.

    DATA r_elemdescr TYPE REF TO cl_abap_elemdescr.
    r_elemdescr ?= cl_abap_elemdescr=>describe_by_name( 'DATA_ELEMENT_HERE' ).
    DATA r_field TYPE REF TO data.
    FIELD-SYMBOLS <field> TYPE ANY.
    CREATE DATA r_field TYPE HANDLE r_elemdescr.
    ASSIGN r_field->* TO <field>.
    <field> = .....
    "now <field> is your dynamic field value
    Regards
    Marcin

  • How do i get value of Environment variables from a java program?

    Hai,
    I have a setup file wherein i set the path for the root directory , in the following way.
    setenv TPEROOT "/home/systpe/khurram/changes"
    when i give echo $TPEROOT
    the path , home/systpe/khurram/changes , is displayed on the console
    If i want to assign the $TPEROOT to a string variable str in my java program , then how can i do it.
    I want to use the value of the environment variable in my program.
    Any help in this regard would be very useful
    Thanks
    khurram

    java -DTPEROOT=$TPEROOT my&#337;ackage.myclass myargs

  • How to read a variable from a java program?

    Hey guys
    I don't actually have a single clue about java itself. I usually use other languages. But i need to get the value of a variable/label from a java program.
    After googling a bit i installed Java Access Bridge to get some more info how to find that value. Using JavaMonkey i got following accessibility info:
    AccessibleContext information at mouse point [0, 0]:
    Name: Spin:
    Description:
    Role: label
    Role in en_US locale: label
    States: enabled,focusable,visible,showing
    States in en_US locale: enabled,focusable,visible,showing
    Index in parent: 0
    Children count: 0
    Bounding rectangle: [421, 152, 598, 169]
    Top-level window name: WEedit
    Top-level window role: frame
    Parent name:
    Parent role: panel
    Visible descendents count: 0
    This label contains a value that i need. I'd like to write it to a textfile periodically or put it into the clipboard. Shouldn't be more than some lines of code.
    Would appriciate any help.
    Thank you and merry christmas

    Nachtschicht wrote:
    Well, i play google for you: http://java.sun.com/javase/technologies/accessibility/accessbridge/
    So you tell me you can't extract any information out of this?
    AccessibleContext information at mouse point [0, 0]:
    Name: Spin:
    Description:
    Role: label
    Role in en_US locale: label
    States: enabled,focusable,visible,showing
    States in en_US locale: enabled,focusable,visible,showing
    Index in parent: 0
    Children count: 0
    Bounding rectangle: [421, 152, 598, 169]
    Top-level window name: WEedit
    Top-level window role: frame
    Parent name:
    Parent role: panel
    Visible descendents count: 0
    You don't have any hint, how i as a non-java programmer can access data out of a simple java program?No. That's a horrible way to interact with a program. A better way would be if it exposed some API. Or there was a web service you could call, for example.

  • Using java class and variables declared in java file in jsp

    hi everyone
    i m trying to seperate business logic form web layer. i don't know i am doing in a right way or not.
    i wanted to access my own java class and its variables in jsp.
    for this i created java file like this
    package ris;
    import java.sql.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class  NewClass{
        public static void main(String args[]){
            Connection con = null;
            ResultSet rs=null;
            Statement smt=null;
            try{
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con=DriverManager.getConnection("jdbc:mysql:///net","root", "anthony111");
                smt=con.createStatement();
               rs= smt.executeQuery("SELECT * FROM emp");
               while(rs.next()){
                String  str = rs.getString("Name");
                }catch( Exception e){
                    String msg="Exception:"+e.getMessage();
                }finally {
          try {
            if(con != null)
              con.close();
          } catch(SQLException e) {}
    }next i created a jsp where i want to access String str defined in java class above.
    <%--
        Document   : fisrt
        Created on : Jul 25, 2009, 3:00:38 PM
        Author     : REiSHI
    --%>
    <%@page import="ris.NewClass"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1><%=str%></h1>
        </body>
    </html>I wanted to print the name field extracted from database by ResultSet.
    but it gives error cannot find symbol str.
    please help me to find right way to do this.
    i am using netbeans ide.

    Very bad approach
    1) Think if your table contains more than one NAMEs then you will get only the last one with your code.
    2) Your String is declared as local variable in the method.
    3) You have not created any object of NewClass nor called the method in JSP page. Then who will call the method to run sql?
    4) Your NewClass contains main method which will not work in web application, it's not standalone desktop application so remove main.
    Better create an ArrayList and then call the method of NewClass and then store the data into ArrayList and return the ArrayList.
    It should look like
    {code:java}
    package ris;
    import java.sql.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class  NewClass{
        public static ArrayList getNames(){
            Connection con = null;
            ResultSet rs=null;
            Statement smt=null;
            ArrayList nameList = new ArrayList();
            try{
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con=DriverManager.getConnection("jdbc:mysql:///net","root", "anthony111");
                smt=con.createStatement();
               rs= smt.executeQuery("SELECT * FROM emp");
               while(rs.next()){
                nameList.add(rs.getString("Name"));
               return nameList;
                }catch( Exception e){
                    String msg="Exception:"+e.getMessage();
                   </code><code class="jive-code jive-java"><font>return nameList;</code><code class="jive-code jive-java">
                }finally {
          try {
            if(con != null)
              con.close();
          } catch(SQLException e) {}
          </code><code>return nameList;</code>
    <code class="jive-code jive-java">    }

  • Dynamic variable declaration

    Hi
    I am writing a procedure in which i get the schema name only as input parameter at run time.
    for e.g.:
    v_id sys.dba_users.username%type;
    In the above datatype declaration, SYS is the schema name, dba_users is the table name and username is the column.
    What if i do not know the schema name up front, is there a way for me to have dynamic declaration?
    p_schema VARCHAR2(30) := 'SYS';
    v_id dba_users.username%type; -- how can i associate this with p_schema
    Is it even possible?

    The bigger picture is this. The same set of tables is copied to new schema once every few months. The tables data structure does not change except for the schema it belongs to. So i have to write a code which will accept the schema name as input parameter and retrieve data from the right tables.
    My question is if there is a way to dynamically specify a schema while declaring variables.
    how can i get the equivalent of v_id sys.dba_users.username%type if schema name 'SYS' is not known upfront.
    If i get to to schema name 'SYS' at run time like below:
    p_schema VARCHAR2(30) := 'SYS';
    v_id dba_users.username%type; -- how can i associate this with p_schema
    If dynamic declaration is not at all possible, i can always declare my variables as follows:
    v_id VARCHAR2(30);
    Thanks everyone for your answers though.

  • Cursor variable in a Java program

    Hi all,
    I would like to know is how to explicitly pass a cursor variable to a stored procedure after defining it in the java source and to get the result back to the same variable.
    An example would be appreciated.
    Thanks in advance
    Giridhar

    I think java profiler can do the job. There are sharewares i know of like JSprint and JProfiler.

  • How to interchange data between Java Program and C++ Program

    I constructed a java program using netbeans IDE that has ability to connect with a respective DLL; I want to connect and interchange data between my java program(.jar) and VSC++ program(.exe) through DLLs.JNI uses single Dll to invoke C/C++ function in native manner,In order to increase the efficiency I tried to connect and interchange primitive data types between Java program and C++ program using that DLL(JNI implemented).
    Unfortunately C++ program cannot obtain data values that has been changed by Java Program.For example - If I declare a global int variable in DLL,java program can catch that int variable and can update it but If I run my C++ program(exe) loading same DLL simultaneously it cannot receive the updated value of that int variable declared in the DLL.
    Therefore I need a solution to share/Interchange at least primitive data and their respective values between a JAVA and C++ Program using JNI (in spite of date transferring through Sockets).JAVA TO C/C++ & C/C++ to JAVA using DLLs.
    (TWO WAY COMMUNICATION)
    JAVA.jar  <=> <JNI> <=> DLL <=> [Connector Program C/C++] <=> C/C++ .exe
    PLEASE HELP ME!! THANK YOU

    What in the are you trying to say? What does "filled into the servlet mean?" Is the map somewhere outside the servlet? In a different process? Are you asking how to extract data from a map? Something else?
    Sorry, but your question makes no sense.

  • Anyone have Huffman Dynamic  coding written in java

    Hi to all
    I am looking for huffman dynamic coding written in java programming. If have it please don't hesitate to help...
    Thankx in advance
    Jenifer

    google for it.
    %

  • Source a file containing variable declaration in csh style into Java

    Hi ALL,
    How can java import a plain-text file which contains csh declaration statements, then simultaneously declare as the statements into the java program?
    Below is the example that the plain-text file:
    set test_1 = ('1')
    set test_234 = ('2' '3' '4')
    set test_abc = ('abc')
    Thanks,
    Alex

    I have already started learning Java from the web site of Sun Java. Thanks for your advise.
    However, let me explain more details what I want.
    Now, I have a plain-text file, such as /mnt/source.txt, which contain statement of variable declaration as below
    ====================
    set test_1 = ('1')
    set test_234 = ('2' '3' '4')
    set test_abc = ('abc')
    ====================
    These are the csh statement.
    In csh, we can use command "source [filename]" then the variable test_1, test_234 and test_abc will be declared directly. Right?
    Then, now, I have this plain-text file. Also the csh statement.
    However, I would use Java to declare the variable test_1, test_234 and test_abc from this file.
    Thanks.
    Alex

  • How to make a Java program that recognises a function of two variables...

    How to make a Java program that recognises a function of two variables to assign values to that?
    First I will give an example and then do the question.
    Ex1.
    We have any function, eg.y = x ^ 2 + 1 (read 'y' equals 'x' high to the square), a function of the second degree.
    To build the graph of this function attach values to 'x' to find the values of 'y'
    And thus mount the pair ordered (x, y) which represents a point on the Cartesian plane.
    Assigning values to 'x' 'we can build up a table that gives us the pairs ordered:
    We can use any numbers, but arfer interval [-3.3]
    X | y = x ^ 2 + 1
    -3 | Y = (-3) ^ 2 +1 = 10
    -2 | Y = (-2) ^ 2 +1 = 5
    -1 | Y = (-1) ^ 2 +1 = 2
    0 | y = (0) ^ 2 +1 = 1
    1 | y = (1) ^ 2 +1 = 2
    2 | y = (2) ^ 2 +1 = 5
    3 | y = (3) ^ 2 +1 = 10
    We then ordered the pairs:
    (-3.10), (-2.5); (-1.2), (0,1), (1,2), (2,5), (3,10)
    Tabem that can be represented by a table:
    X | y
    -3 | Y = 10
    -2 | Y = 5
    -1 | Y = 2
    0 | y = 1
    1 | y = 2
    2 | y = 5
    3 | y = 10
    Now I begin to explain my doubts.
    See this program:
    Ex2
    * To change this template, choose Tools | Templates
    * And open the template in the editor.
    Encontrando_o_valor_de_y package;
    * @ Author des Soldat Gottes
    Import javax.swing.JOptionPane;
    Public class (Main
    * @ Param args the command line arguments
    Public static void main (String [] args) (
    Int x, y;
    String x1;
    X1 = JOptionPane.showInputDialog ( "We have the function y = x + 1 \ n" +
    "Assign a value for 'x',"); / / receives a value for the function y = x + 1
    X = Integer.parseInt (x1); / / tranforma String in int
    Y = x + 1; / / receives the value of 'x' and calculates' y '
    JOptionPane.showMessageDialog (null, "The value of 'y' is: \ t \ t" + y);
    / / Displays the value of 'y'
    System.exit (0);
    We see that the program receives above a value for 'x' and replaces the function contained in the program, y = x + 1, and so is the value of the variable 'y'.
    In: x1 = JOptionPane.showInputDialog ( "We have the function y = x + 1 \ n" +
    "Assign a value for 'x',");
    The entry is a number and that number is assigned aa ja existing function in the (y = x + 1).
    The question is: would it be possible to come to a function?
    Ex: the program ask: DIGITE THE FUNCTION?
    The USUARIO DIGITARIA A FUNCTION ANY, TYPE: y = x ^ 2 +1
    The program would recognize the function and give numerical values to that function as Ex1, at the beginning of this text.
    And then to find the values of the x and y launch a table.
    It would be possible that?
    By invez of entering with a number so that the program sustitua a function ja existing as Ex2, seen above, entering with a function quaquer (type: y = x ^ 2 +1) for the program atribuisse values to that function and then create a table of values as Ex1.
    I hope it has been easier to understand my doubts now.
    Thank you for your attention!
    God bless!

    rafaelmenezes wrote:
    Thanks for the explanation, could understand what fly said.
    But as it applied to a program?
    How to create a program that recognizes that the entry coefficients?Are you asking about how to parse out the coefficients from the string "3x^4 + 4x^3 - 8x^2 + 5x^1 + 2x^0"? If you define the format to strictly follow that example, this should get you started:
    Strip out the spaces
    Split the String on "x^"
    That should give you [3, 4+4, 3-8, 2+5, 1+2, 0]
    Split each resulting String on "+ | -", preserving the operator as a token so you can apply the correct sign to the coeff.
    That should leave you with [3, 4, +, 4, 3, -, 8, 2, +, 5, 1, +, 2, 0]. Every other number is a coeff, the rest are the degrees.
    You can strip out the +, since those coeffs are already positive, and strip out the - after negating the following number. This is all assuming that you have to write this yourself. There is no doubt already a library or 5 out there that does this for you.

  • How to Set up the  variables and others to compile and Run Java Programs

    Hello,
    I have just downloaded the jdk1.6.0_07 and jre1.6.0_07 and installed it in C:\Program files\Java in my Windows XP ,So please tell me how to sett up the enviroment variables etc to compile and run Java Programs from Command Prompt.
    thanks

    To set the PATH permanently, add the full path of the jdk1.6.0_<version>\bin directory to the PATH variable. Typically this full path looks something like C:\Program Files\Java\jdk1.6.0_<version>\bin. Set the PATH as follows on Microsoft Windows:
    1. Click Start > Control Panel > System on Windows XP or Start > Settings > Control Panel > System on Windows 2000.
    2. Click Advanced > Environment Variables.
    3. Add the location of bin folder of JDK installation for PATH in User Variables and System Variables. A typical value for PATH is:
    C:\Program Files\Java\jdk1.6.0_<version>\bin

Maybe you are looking for