Pass A Variable From One Class To Another

Jan 5, 2014

As the title says, how can I pass a value from one variable to another class?

Example:

So here I'm switching one frame to another, called "redigeraProjekt". Now in this class, I want the value from .getSelectedIndex() pass over to that class. I've tried to use the variable "valIListan" but it cannot find it. Probably because it's "private" (?)

valIListan = listaAllaSpelProjekt.getSelectedIndex();
redigeraProjekt npj = new redigeraProjekt(); // "switching" to another frame
npj.setVisible(true);
this.setVisible(false);

I hope you understand what I'm saying

View Replies


ADVERTISEMENT

Pass Variable In Java From One Class / Jframe To Another

Apr 3, 2015

Right so I have my ItemsPage Jframe Class and I'm trying to pass my TotalPrice variable to my CashPay so I can calculate the change. CashPrice runs and works but when I try run ItemsPage it does nothing I don't even get errors. I tried to remove that small section of trying to pass the variable to CashPay and it worked perfectly so I 100% know that's the problem. This section "public ItemsPage() { Pounds = ""; //super();

Scanner sc = new Scanner(System.in);
CashPay sendTotalPrice = new CashPay();
//System.out.println("Enter your amount");
TotalPrice = sc.nextDouble();
sendTotalPrice.printTotalPrice(TotalPrice);
"
Here is complete code for both classes, I'm using GUI builder.

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

[code].....

View Replies View Related

Pass Private Final Class Object To Another Class Constructor

Aug 28, 2014

can we pass private final class object to another class constructor?

View Replies View Related

Pass A Value From Main Class To Another Class

Feb 5, 2015

THIS IS MY MAIN CLASS:

Java Code:

import java.io.FileNotFoundException;
public class PrimaClasse {
public static void main(String[] args) throws FileNotFoundException {
SecondaClasse oggettoSeconda = new SecondaClasse();
oggettoSeconda.controlloNomi();

[code]....

Now it's working and from the main class i can controll the second class BUT. i want that is the main class that ask the user name, and i want pass that value to the second class. what code i must change?why eclipse wants me insert this import java.io.FileNotFoundException; and this throws FileNotFoundException for not give me an error?

View Replies View Related

JSF :: How To Pass Value To Bean Class

Nov 7, 2014

Getting JS -> JSF value to Bean class? I have tried with below code:

JSF code:

<h:commandButton styleClass="buttonpos" value="Get Value" action="#{myBean.action}" onclick="document.getElementById('crForm:hiddenInput').value">
<h:inputHidden id="hiddenInput" value="#{myBean.action}"/>
</h:commandButton>
Java Beans
public class CurrencyExchangeBean {
protected static Logger logger = ILogger.getLogger(CurrencyExchangeBean.class);

[Code] .....

View Replies View Related

JSP :: Pass Collection Object From Tag Class

Apr 12, 2014

I am not able to get the arraylist object on jsp. Below is my code.

public class DisplayArticle extends SimpleTagSupport
{
String path;
List<Article> articleList=new ArrayList<Article>();
public String getPath() {
return path;

[code]....

View Replies View Related

Servlets :: How To Pass Bean Class Instance

May 10, 2014

here i have my bean class

package com.emp;
public class salarybean {
private String name;
private Double days;
private Double id;
public String getName() {
return name;

[code]...

now i want to retrieve all these values in another servlet where i want to do some calculation but not able to retrieve it is showing null and indicating for this value in my eclispe IDE " Iterator<salarybean> itr=list.iterator(); "

public class Time extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException

[code]....

View Replies View Related

JSP :: How To Pass Attribute Along With Its Value From Servlets To Plain Java Class

Nov 3, 2014

I just want to know how to pass an Attribute along with its value from servlets to a plain Java class. Im using java beans but it's showing null...So as an alternative im using session to access variables in java class..

View Replies View Related

Enum Class - How To Pass In Faction Type In For Daunt

Mar 6, 2015

Suppose I have an enum class named Faction and one of the constants is named DAUNT. I created a class of the enum DAUNT but how can I pass in a DAUNT faction type in for Daunt?

Java Code:

public enum Faction {
ALMIGHTY, AMBITION, DAUNT, RESTLESS, CAN;
}

//new file
public class Daunt {
public Daunt() {
}
} mh_sh_highlight_all('java');

View Replies View Related

Pass Objects From 3 Different Methods At Random To Different Class And Store In Array?

Jun 8, 2014

in my progrm there are three diff array of objects...namely garments..gadgets and home app...now one who buys from each of these sections will have to make a bill at last...when he choses to make the bill he will be shown the list of products he bought and their details (like price...brand...etc)...so i thought that while he orders each product(which is done in a previous method called purchase()...)....(each product is stored as an object in there diif arrays namely garments...gadgets ...appliances)....each of those object will be copied in a new array in a diif class...then that array print will give me the desired result...

is this approach correct...?and if its correct then how can i pull out a specific obj frm a stored array of object and then save it in a new array....?

View Replies View Related

Error Passing Value Of A Variable From One Class To Main Method Of Another Class

Jan 8, 2014

I've 3 classes.

1. Circle
2. GetInputFromUser
3. testCircle

package ABC;
public class Circle {
private double radius;
public double getRadius() {
return radius;

[Code] .....

In the testCircle class, in the line: getRadius = ui1.GetInput();

It's showing the error: The method GetInput(float) in the type GetInputFromUser is not applicable for the arguments ()

And when I do: getRadius = ui1.GetInput(rad);

It's showing the error: rad cannot be resolved

View Replies View Related

Enterprise JavaBeans :: Why Entity Class Received As Null When Pass Through Remote EJB

Oct 23, 2013

I have an stand alone client to use a remote ejb with some methods. All works fine, but when I pass an entity class as parameter from the stand alone client to the remote ejb, the entity class is received as null.

View Replies View Related

Using A Variable From Main Class In Another Class

May 31, 2014

I have a program with 4 classes, all of them in the same package, one of them is the Main class, and in that class I declared a variable named "port" of type int. One of the 3 another ones is the class Connection class, which it requires the port variable. I want to use this variable in the Connection class. How can I do it?Both classes are shown below:

Main.java
package server;
/* Imports */
/* Another variables */
int port; /* <-- IS THIS ONE */

[code]....

View Replies View Related

Static Variable Within A Class?

Dec 8, 2014

I have a class Tree in which all the methods to build a tree are in place. But however I would want variable of by Tree which is pointing to the last node being added to the tree.

So basically every time you keep adding a node to the tree you tail pointer gets updated to point to the last node. I have this so far.

public class NonEmptyTree implements Tree {
private Tree left;
private int data;
private int leftleafCount;
private int rightleafCount;
private Tree right;
private Tree tail; // This variable must be shared by all the object. There needs to just one tail pointer to the tree.
public Tree insert( data ) {
tail = // gets updated every time when new node gets added.

View Replies View Related

How To Transfer The Value Of A Variable From One Class To Another

Mar 12, 2015

I am developing a program that seems to need to transfer the value of a variable from one class to another. However, neither of the classes is the main class.

This simplified code should make my problem clear:

This is my main class where I create runone from Class1 and runtwo from Class2:

public class MainClass {
public static void main(String[] args) { 
Class1 runone = new Class1();
Class2 runtwo = new Class2(); 

}

This is Class1 where I set up x and give it the value 20. I also have a getter-method to be able to access the value of x from other classes.

public class Class1 {
int x = 20;
public int getX(){
return x;
}}

This is Class2, where I try to access the value of x that was set up in runone.

public class Class2 {
int y;
public Class2() {
y = runone.getX + 10;
System.out.println(y);
}
}

Unfortunately, I get the error message "runone cannot be resolved to a variable."

What do I do to be able to access the variable x (that is set up in Class1) from Class2?

View Replies View Related

How To Implement A Variable Into Another Class

Apr 13, 2015

I would like to implement a variable in a class that is used in another class, how can I do that?

I have a Mall class and a Customer class I would like to associate the position of the customer that is in the Mall class and also implement the same in the Customer class.

Here is a small part of the code without all the methods.

//the two objects
Mall m = new Mall("RandomMall",50,30);
Customer c1= new Customer("Jack",1240);
//the first part of the mall class without the methods
class Mall{
private String name;
private int width,length;
String[][]grid = new String[width][length];

[code]...

View Replies View Related

Passing In Variable From One Class To Another?

Aug 16, 2014

It just wont work... Everytime I try to pass a variable into a class it sets it to 0. What to do.

The variable should be '2', but it prints out as 0!?

Java Code: package Zoo;
import java.util.Random;
public class Animal {
Random random = new Random();
public void Eat(){
System.out.println("The animal starts to eat.");

[Code]...

The method bash, needs the variable 'playerTwo' to be 2, to carry on. But it prints out 0.

Int the animal constructor, I passed in the variable from another class, and it prints out 2 in the constructor like it should. But outside of the constructor it is equal to 0.

View Replies View Related

Each Class Contain Private Variable

Sep 22, 2014

I'm working on a project that contains multiple classes. Each class contains and must contain only PRIVATE variables. Here's my issue. When my test code calls for a new instance of "StudentClass" as so:

StudentClass studentClass = new StudentClass(offeredClass.getClassIdNumber(),
offeredClass.getClassName(),
offeredClass.getClassroom());

The corresponding constructor won't let me initialize it's variables because they are declared private within another class, as shown here:

StudentClass (float classIdNumber, String className, Classroom room){
this.classIdNumber = classIdNumber;
this.className = className;
this.room = room;
}

When getClassName, getClassroom, and getClassIdNumber are passed to a toString() method elsewhere in my test code. the output is returned just fine. When passed through the StudentClass, I'm getting Null across the board.

View Replies View Related

Get The Value Of A Variable Stored In Another Class Of Another Package?

Nov 12, 2014

Source packages

1.First package : CarDealer.java (JFrame form)

2. Second package : Vehicle.java (JFrame form)

- The user runs CarDealer JFrame in which appears a JTexField in which he types in some characters (pretending he types some data about selling licence).
- The user clicks on the JButton "Save" that stores the characters typed in inside a String variable called strLicence.
- The user clicks on the JButton "Open 'Vehicle Form'" that opens the Vehicle JFrame.
- Finally, in the Vehicle JFrame I want to write a code that can see/use the value of the String variable called strLicence.

In other words, for example, I want to store the value of strLicence in a new variable declared in the Vehicle class.

(Actually I'm working with a long and elaborate project for the dissertation of my bachelor's degree, so, for cutting to the chase, I simplified it by creating an essential JFrame project.)

I used netbeans

1. CarDealer.java

package Firstpackage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;

[code]....

I also attach the netbeans project in a zip file ....

View Replies View Related

Sandwich Class Static Variable

Apr 14, 2015

Sandwich class. I have thus far completed creating a sandwich class with a seperate sandwich Tester class to run with it. (this is according to the assignment). Now I must create Static variables for the sandwich class:

Add two static variables to the Sandwich class to count how many sandwiches are sold and how many slices of tomato are used. Initialize each to 0.Where do you add code to increment the sandwich counter? Determine this and then add code.

public class Sandwich
{
static int numOfSold = 0;
static int slicesUsed = 0;
private String meat;
private int numOfSlicesOfTomato;
private boolean lettuce;

[code]....

View Replies View Related

Class Cannot See Its Own Protected Variable From Another Package

Jan 30, 2014

how access levels work from subclasses and other packages, and I have discovered that a class cannot see it's own protected variable from another package which I thought it would be. I know that it says in the java docs "The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package." but I thought that would also include it's own class.

Java Code:

package food;
import food.fruit.*;
public class Food {
protected int protecte = 5;
private int privat = 5;
protected void method(){

[Code] ....

View Replies View Related

Cannot Access Variable Form Another Class

Jul 24, 2014

why I cant access a variable from another class, in particular why in the main class cant i assign " quiz1 = getQuizOne();" ? It constantly giving me error.

Java Code:

import java.util.Scanner;
public class Grade {
private int quiz1, quiz2, midtermExam, finalExam = 0;
public static void main(String[] args)
{
Student John = new Student();
John.StudentData();

[code]....

View Replies View Related

Variable From Superclass Using In JFrame Class?

Oct 9, 2014

I would like to pass a variable I have in my main to my JFrame. In my main I calculate which pictures I should show in my JFrame. Is it possible to make objects of these pictures in my main class and use them in my JFrame? something like my code below.

public class JFrameTesting {
 public static void main(String[] args) {
Image ImageVariable = Toolkit.getDefaultToolkit().getImage("C:1.jpg");
 MyJFrame f = new MyJFrame();
f.setTitle("Drawing Graphics in Frames");

[code]....

View Replies View Related

How To Inject Variable That Is Instantiated In One Class Into Another

Mar 19, 2014

Let's say I have a Junit4 class FooTest.java and variable token. At some point in class the token gets instantiated.

Java Code:

public class Foo {
private String token;
@Before
public void setUp() {
// serious is steps to get the application to a certain state where token can get extracted
....
token = extractToken();
}
} mh_sh_highlight_all('java');

Now I have another class User.java where I need to use this token. Can I inject this token somehow into that class? There is no relation between Foo and User. I can't use Provider method in the configure file because extraction of the token depends on certain system's state and can't be called anywhere anytime.

View Replies View Related

Difference Between Instance And Class Variable

Mar 25, 2014

I'm having trouble to fully understand the difference between instance and class variables.

I though it was all about the static, i.e:

int age; // instance variable
static int age; // class variable

What's behind this?

View Replies View Related

All Instances Of Same Class Change Each Other Private Variable

May 2, 2015

I have a class named Base and a private variable named _hopcount i have 10 instances of class base i use _hopcount as creteria to some if but other instances edit _hopcount so i want to prevent _hopcount edit by other instances; I want to have private variable which other instances of same class can't modify it.

public class Base extends TypedAtomicActor {
private int _hopcount = 0;
if(_hopcount <= 3) {
some code;
}
public function() {
_hopCount += 1;
}
}

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved