Home Answers Viewqa Java-Beginners How to pass variable from one class to another in java

 
 


ratna rathor
How to pass variable from one class to another in java
1 Answer(s)      a year ago
Posted in : Java Beginners

How to pass variable from one class to another in java?

View Answers

May 17, 2012 at 5:51 PM


Example:   

public class Class1 {                       //Class1
    private String name = "Class1";
    private int IdNo = 20;

    public void class1Method() {
        System.out.println("Welcome in " + name + " Its id number : " + IdNo);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getIdNo() {
        return IdNo;
    }

    public void setIdNo(int idNo) {
        IdNo = idNo;
    }

}

//Another class Class2.

public class Class2 {   
    public static void main(String[] args) {
        Class1 class1 = new Class1(); // Creating object of Class1
        String name = "Class2";
        System.out.println("Welcome in " + name);
        System.out.println("Another Class variables value:" + class1.getIdNo()
                + " and " + class1.getName());
           class1.class1Method();
    }
}

Description: In Class1, there is two private variables one is String type and another is int type. Since these variables are private so we can?t access these directly into another class. So we will create getter and setter method of these variables. In Class2 ,create object of Class1 as Class1 class1 = new Class1(); Now you can use its methods and variable through by calling Class1 getter methods. If there is public variable in Class1 then you can access them directly by Class1 object as class1.name.









Related Pages:
How to pass variable from one class to another in java
How to pass variable from one class to another in java  How to pass variable from one class to another in java?   Example: public class...; } } //Another class Class2. public class Class2 { public static void main(String
how to pass class variable to another class
how to pass class variable to another class  i need to access a variable from a class to another class currently running at the same time. using... the following code: import java.util.*; class A { static int regd; static int
how to pass a string from one class to another
how to pass a string from one class to another  hi all, Good morning. I am trying to develop a coding pass a string from one class and declare... string from one class to another. import java.util.*; class A{ static String
How To Pass data from one GUI to another in java swing
How To Pass data from one GUI to another in java swing  I'm new to java and part of our assignment is to build a GUI and display a result set from data input. I'm stuck at how to get the user's input from JTextFields and combobox
how to pass an array from one jsp to another jsp - JSP-Servlet
how to pass an array from one jsp to another jsp  hi friedns, can any One tell me how to send an array from one jsp to another jsp,pls any one send the code for this.also porvid the code how to retrive the arry in another jsp
how to pass arraylist data from one jsp to another jsp - JSP-Servlet
how to pass arraylist data from one jsp to another jsp  hi to all iam not able to pass the data which is stroed in arraylist, actually i need... the following >XXX/ ? is required to send a value from one jsp to another
how to pass variable from simple java main class(not servlet) to the jsp page?
how to pass variable from simple java main class(not servlet) to the jsp page?  I have a simple Java class in which I invoke a call to a JSP page... from the simple Java class to the JSP page. How can I do that? Here is my
passing data from one jframe to another
passing data from one jframe to another  I have two JFrame,built by using the GUI Editor netbeans 6.9.i have to pass a data from a Jtextfield in the first Jframe to another JLabel in the other JFrame.Can you please help me,how
how to go from one frame to another frame in swings - Java Beginners
how to go from one frame to another frame in swings  Hi, My task is to go from one frame to another.......... let us think that iam having two... this task in swings or applet......... i have gone through one example of login
how to write function of copy from one to another location in this code
how to write function of copy from one to another location in this code  I need to write a code to copy a file from one location to another using... class Browse extends JFrame implements ActionListener { JButton button1
Pass cookie from one site to another within the same domain
Pass cookie from one site to another within the same domain  Hi, I want to pass a cookie from one website to another within the same domain. This is just to identify whether the same user is logged in or not. Can anyone help me
How to navigate from one jsf to another in eclipse
How to navigate from one jsf to another in eclipse  Hi there is my jsf code... <%@ page language="java" contentType="text/html; charset... submitting the form it navigate to the following page :- <%@ page language="java
Java - Copying one file to another
Java - Copying one file to another       This example illustrates how to copy contents from one... the contents from one specified file to another specified file. copyfile(String
File copy from one directory to another in java
for copying the file from one directory to another directory in Java...File copy from one directory to another in java  I am trying to find very good code example for copying a file from one directory to another
Example of a class variable (static variable)
of a static variable first of all create a class StaticVariable. Define one static... Example of a class variable (static variable)       This Java programming example will teach you how
Pass values from form to form
Pass values from form to form       Java program to pass values from one form to another form... values from one page to another consecutive page which is being called then we can
how to copy file from one directory to another in java with examples
how to copy file from one directory to another in java with examples  how to copy file from one directory to another in java with examples? I want... copy from one directory to another in java. Thanks
Moving file or directory from one directory to another
Moving file or directory from one directory to another... directory is moved from one directory to another directory. This program... from one directory to another. In this section an example is given
Java - Copying one file to another
how to copy contents from one file to another file. This topic is related... Java - Copying one file to another   ... called copyfile which copies the contents from one specified file to another
How to write content of one file to another file.
the use of CheckedOutputStream class. The CheckedOutputStream class is from the java.util.zip package. The CheckedOutputStream  class is used.... And at the same time the CheckedOutputStream  class calculates checksum
pass a variable(friends name) through a hyperlink in to another jsp.
pass a variable(friends name) through a hyperlink in to another jsp.  ... in to the next page,but i want to pass a specific friend name which i was clicked,plz tell me how i do this? thanks
pass a variable(friends name) through a hyperlink in to another jsp.
pass a variable(friends name) through a hyperlink in to another jsp.  ... page,but i want to pass a specific friend name which i was clicked,plz tell me how i do this? thanks
how can we pass parameters from jsp href to another servlet
how can we pass parameters from jsp href to another servlet  how can we pass parameters from jsp href to another servlet
How to copy files from one folder to another based on lastmodified date - Java Beginners
How to copy files from one folder to another based on lastmodified date  I have to make a simple utility to copy files from a folder to another... java.io.*; public class CopyFile{ private static void copyfile(String srFile
how to change password into one jsp to another jsp
how to change password into one jsp to another jsp  i have two jsps in one jsp i give one password and confirm password, in another jsp i want change my password compare with 1st jsp how to write code please give me answer?  
get the value from another class - Java Beginners
get the value from another class  Hello to all, I have stupid question. I have main.java and ConfigXML.java(read my config xml file). Code from...().trim(); [/code] How I can get String alsl = ((Node)flnameTEXT.item(0
Copy one file into another
Copy one file into another In this section, you will learn how to copy content of one file into another file. We will perform this operation by using...;This string is copied from one file to another\n"); bf.close(); InputStream
problem in sending data from one ip to another ip in JAVA
problem in sending data from one ip to another ip in JAVA  Hi guys . this is naveen kumar...i need a help. i want to connect one ip to another ip... device in JAVA. i know how to ping but don't how to send the msg's to another IP
File copy from one drive to another. - Java Beginners
File copy from one drive to another.  I want to copy a .mp3 file from one drive(let d: drive)to another drive(let e: drive) in my Windows XP. How can I do
How to forward the control from one jsp to another?
How to forward the control from one jsp to another?  Hi! This is Prasad Jandrajupalli. I have the 3 JSP's, but I want communicate with each... is not communicate with the Third JSP. I want forward the control from first jsp to second
you pass a variable by value.
you pass a variable by value.  How do you pass a variable by value... $a = &$b /*--------------Pass By value----------------*/ function add($a) { return ++$a; } add($a); /*---------------Pass by reference
Use varibles from another class
Use varibles from another class  **I have two classes. class 1:** public class askfigures extends JFrame { method from class1: public void... in another class. another class: public class Askvalues extends JFrame
How I get a variable from java script to use it in the scriptlet of jsp.
How I get a variable from java script to use it in the scriptlet of jsp. ...;class=moreRewards&dealID=27"; var tinyUrl; BitlyCB.shortenResponse = function... we need to grab the first one. for (var r in data.results
Drag and Drop components from one frame to another frame
Drag and Drop components from one frame to another frame... button etc.) from one frame to another frame. In this code we have used...(); } } Compile this java code with javac command  from command
Copying an array to another
Copying an array to another       Java Copy Array Example:In this tutorial, you will learn how to copy data from one array to another. Here, providing you an example with code
Copy data from one JTextField to another JTextField
, you will learn how to copy the data from one JTextField into another... Copy data from one JTextField to another JTextField... and then paste into another JTextField. JTextField is used in the swing
ArrayList as an argument to another class
ArrayList as an argument to another class  how do you pass arraylists as arguments to another class, and how does the method/class accept this arraylist for use
Class
and Perimeter of the square and another class is rectangle which is using for getting... one it's own constructort. Constructor creates a instance for the class... that how to to implement the constructor feature in a class. This program
How can i pass the valus from a JSP to the action class???
How can i pass the valus from a JSP to the action class???  hewllo wevryone... can anyone help me with how i can pass the value of menuId in my JSP and pass it in the action class
How to copy files from one folder to another based on lastmodified date - Java Beginners
Copy files from one folder to another  How to copy files from one folder to another based on lastmodified date
Java call method from another class
Java call method from another class       In this section, you will study how to access  methods of another class. For this we have created two java files
How to write content of one file to another file and calculate checksum for accuracy.
. void read(byte b) Method read one byte of data from...CheckSum Calculation in java This example calculate the checksum...; compute checksum value as data read from associated stream
Pass parameters from JSP to Servlet
Pass parameters from JSP to Servlet In this section, you will learn how to pass parameters from JSP page to servlet. For this purpose, we have used...: <%@page language="java"%> <% request.setAttribute("Company
Assigning a value to JSP variable from a bean class. - JSP-Servlet
Assigning a value to JSP variable from a bean class.  Hi, I want to know how we can assign value to a JSP variable from a Java Bean. I am using Struts, JSTL. Which tags should I use to assign the value. e.g Kindly
calling one jsp from another jsp - JSP-Servlet
calling one jsp from another jsp  Hi All, In my web application... in two.jsp by using jsp declarative tag. Now from one.jsp file I want to call... start from where we call this two.jsp files method. i.e. in one.jsp file at line
swings:how to link from one form to another form
swings:how to link from one form to another form  how to link from one form to another form
Using Unicode Variable Names - Java Tutorials
Using Unicode Variable Names 2001-11-23 The Java Specialists' Newsletter... :-) How do you go from being an OO beginner to an OO guru? Simple answer... members are currently subscribed from 56 countries Using Unicode Variable
navigating the value of JTextField from one JFrame to another - Swing AWT
navigating the value of JTextField from one JFrame to another  hello I m doing one project where I m retriving the data from databse in JTextField... first record is comming... how can I fetch all the records from database using up
how to pass string array from action class to jsp page? pls reply me.
how to pass string array from action class to jsp page? pls reply me.  how to pass string array from action class to jsp page? pls reply me.  .... In your action class, String[] arr = {"A","B","C","D"}; for (int x = 0; x < arr
How to call the run method from another class?
How to call the run method from another class?  Sorry, if this is a stupid question. I would like to find out how to call the run method... run() { new FereastraPrincipala().setVisible(true); from the class