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
ADVERTISEMENT
Sep 10, 2014
I need to inject a DataSource, but the catch is the JNDI isn't known until Runtime. I was hoping the EJB could get a System Property of the JNDI name and use that to Inject the DataSource.
For example:
@Resource(name=myRuntimeJNDIName)
DataSource myDataSource;
And I could either @Inject or on postContstruct() set the myRuntimeJNDI variable to the JNDI name that is configured on the App Server via System Properties or some other mechanism. But the JNDI may be different depending on what environment the application is deployed to.
Is this possible? I would like to have the same EAR deployed in all our environments and not have to create a different version of the application for each environment.
View Replies
View Related
Feb 14, 2014
Interfaces are 100 % abstract classes.They cannot be instantiated.Their sole purpose is to be implemented.So why does the following code works just fine while it is attempting to instantiate an interface.
interface TestA { String toString(); }
public class Test {
public static void main(String[] args) {
System.out.println(new TestA() {
public String toString() { return "test"; }});
}
}
View Replies
View Related
Oct 17, 2014
I am being told to (assignment) create a new Bread Object in another class than the original. But after that it is asking for get and set methods. Get and set methods of what? It's parameters?
class Bread
{
private static String breadType; //private field to hold bread type
private static int numberOfCaloriesPerSlice; //private field to hold calories per slice
public static String getBreadType() //get method
{
return breadType;
[code]....
View Replies
View Related
Dec 1, 2014
I have a Member class and a Player class. Players extends Members. I know that player will have all the methods of Member but I was wondering how I could link a player object directly to a member object.
public class Member(){
String name,surname;
public Member(String name, String surname){
this.name = name;
this.surname = surname;
[Code] ....
What im looking for is the p1.getName() to return John. Can I do something within the Player class for it to inherit from a Member object...
View Replies
View Related
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
Jun 5, 2014
I have a JSF bean which is request scope and corresponding JSF UI page. when user tries to open this page, we are getting exception 'Cannot instantiate user.java class <default constructor>'. This does not come always. It comes very rarely. JSF version is 1.2
View Replies
View Related
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
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
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
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
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
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
View Related
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
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
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
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
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
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
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
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
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
Mar 23, 2015
I'm getting this error, I definitely know that is my error trying to pass method/variable because when I commented received part of my code it ran and worked.
I get this error
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Here are my 2 classes
Java Code:
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;
import java.awt.event.ActionListener;
[Code] .....
View Replies
View Related
Feb 15, 2014
What happens to a static variable that is defined within a method of a class ?
View Replies
View Related
Oct 10, 2014
I've got an abstract class
public abstract class AbstractClass
{
//stuff
}
And a few classes that inherit from it
public class Class1 extends AbstractClass
{
//stuff
}
public class Class2 extends AbstractClass
{
//stuff
}
within another class I have a private variable with the type of the Abstract class, and within one of the methods I assign an object to the the variable like this:
public class Test
{
private AbstractClass temp;
public testMethod(){
Class1 anObject = new Class1();
temp = anObject;
}
}
Is this legal? Will temp become a Class1 object?
View Replies
View Related
Feb 26, 2015
public static void main(String[] args) {
String str1="Java";
str1="one";
System.out.println("str1="+str1);
}
String object is stored in a private final char array in String.java.
private final char value[];
The basic characteristic of a final variable is that it can initialize a value only once. By marking the variable value as final, the class String makes sure that it can’t be reassigned a value.
so the String objects can be initialized only once but the above code shows that str1 was initialized first with "Java", then it can be re-assigned value "one" bcos the output is one. If it can be re-initialized, basic characteristic of final variable is not satisified and hence how can we call String objects are immutable?
View Replies
View Related