Accessing A Method From Another Class

Apr 1, 2014

I have an admin class that needs to access a method of another class and I'm unsure how to do it.

One of the methods in the admin class (DancerAdmin) accesses a .txt file with information in and each line is to be extracted and is to be created as an object of the Dancer class. The information in each line is to then be used to set the variables in the Dancer class.

To set the values the Dancer class has setter methods which I need to access each time a new object is created while cycling through the .txt file. I'm struggling to access these methods from the DancerAdmin class when I run the relevant method.

The snippet of code I have from the method in DancerAdmin is

while (bufferedScanner.hasNextLine()) {
currentLine = bufferedScanner.nextLine();
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
dancer.add(new Dancer());
Dancer.setName(lineScanner.next()); mh_sh_highlight_all('java');

I get an error saying non static method setName cannot be referenced from a static content?

View Replies


ADVERTISEMENT

Accessing Parent Class Method Using Child Class Object?

Feb 4, 2015

I want to know is there any way we can call parent class method using child class object without using super keyword in class B in the following program like we can do in c++ by using scoop resolution operator

class A{
public void hello(){
System.out.println("hello");
}
}
class B extends A{
public void hello(){
//super.hello();
System.out.println("hello1");

[code]....

View Replies View Related

Accessing Static Method In Another Class

Apr 6, 2014

I am getting an error trying to access a static method of another class...theyre both in the same package, I've tried importing the class.

I've tried to do A b=new A()
and then
b.evaluate();

Everything that I try I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: B$A
Caused by: java.lang.ClassNotFoundException: B$A
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Code :

public class A{
public static String evaluate(String op) {
}
}
public class B{
String output=A.evaluate(input);
}

View Replies View Related

Accessing ArrayList Of Another Class

Oct 19, 2014

What I want to do is this, this is my first class:

public class Footballer {
int goals;
String surname= "";
String team="";
private static int counter=0;
private int dres;
}

(this is just the header of the class... And this is my second class, which contains an ArrayList of the first class:

public class FootballTeam{
String teamname="";
String league="";
ArrayList<Footballer> f;
}

And this is my third class which contains an ArrayList of the second class:

public class FootballLeague{
String leaguename="";
ArrayList<FootballTeam> ft;
}

What I want to do is, know how many of footballers are there in the league? Meaning how many of "f"s are in the "ft"... I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.

I'm trying this method to get the size of the array in the 2nd class, from the 3rd class (containing an ArrayList of classes of 2nd class, but no luck:

int counter=0;
for(int i=0;i<this.ft.size();i++) {
counter+=this.ft[i].f.size();
}

I'm getting this: Array required, but ArrayList<FootballTeam> found ---

View Replies View Related

Accessing ArrayList Of Another Class

Oct 19, 2014

What I want to do is this, this is my first class:

Java Code:

public class Footballer {
int goals;
String surname= "";
String team="";
private static int counter=0;
private int dres;
} mh_sh_highlight_all('java');

(this is just the header of the class, just how it looks)...

And this is my second class, which contains an ArrayList of the first class:

Java Code:

public class FootballTeam{
String teamname="";
String league="";
ArrayList<Footballer> f;
} mh_sh_highlight_all('java');
And this is my third class which contains an ArrayList of the second clas:
Java Code: public class FootballLeague{
String leaguename="";
ArrayList<FootballTeam> ft;
} mh_sh_highlight_all('java');

What I want to do is, know how many of footballers are there in the league? Meaning how many of "f"s are in the "ft"... I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.

View Replies View Related

Accessing Variables Within A Class?

May 19, 2013

I wanna know which is the best way to access a variables within a class ...

Using direct access or using the variable accessor ...

Example :

public class Numbers{
        private int n1;
        private int n2;
     public void setN1(int n){
     n1 = n;

[Code] ....

View Replies View Related

Accessing Object Created In Another Class

Mar 10, 2014

I have a situation where I have 2 classes and an array of objects which are causing me trouble.

The object type is one I have created - it is made from a class which is neither of the 2 classes I previously mentioned.

The array is created and occupied in Class1 and the problem arises when I try to reference one of the element from Class2.

At first I forgot the the array would be local to Class1.main so I made the array a global variable using:

Java Code: public MyObjectType[] myArray; mh_sh_highlight_all('java');
Then I tried accessing an element (2) from Class2 using:

Java Code: Class1.myArray[2] mh_sh_highlight_all('java');
However I get errors saying that I can't access the static variable from a non-static context.

I understand a little bit about static and non-static objects/methods but don't know how to fix this. Do I need to include "static" in the array declaration?

View Replies View Related

Accessing Private From Driver Class

May 14, 2015

I came across the below

1) When a variables are declared "Private" How should it be accessed from the driver class ? Sometimes i get an error in driver class saying "your variable is declared Private" why am I getting this error ...

The document says "Private" declared variables should be accessed only through methods. What does that mean.

View Replies View Related

Accessing String Values In Class

Apr 9, 2014

What does the line Console[] conList mean? How am I going to access the string values in class consolelist?

class ConsoleInfo

private String conTitle;
private double conPrice;
private int conQty;
private String conPic;
 
[Code] .....

View Replies View Related

Accessing Variables From Array Instance Of Class?

Jan 26, 2015

I am having some problem accessing variables from an array instance of a class. Heres what i have done;

In the main class:

Example obj[]= new Example[4];

In the main class constructor:

obj[0] = new Example(0);
obj[1] = new Example(1);
obj[2] = new Example(2);
obj[3] = new Example(3);

In the main update() method:

if(condition)
//update

In the Example class constructor:

private boolean change = false;

In the Example class update() method:

if(x >20)
change= true;

Now, i want to access the variable change from the main class, how do i do it? The 'condition' in the if statement is the condition of wether the change variable ia true or false. How do i access it?

View Replies View Related

Accessing Private Field Of Derived Object In Base Class?

Apr 1, 2013

I have this piece of code I wrote a while ago to test something. The issue is accessing a private field of Base class in Base but of a Derived object.

Here is the code:
class Base
{
private int x;
public int getX()

[Code]....

The commented code does not work but casting d to Base does.

Forgot to mention that the compilation error is that x has private access in Base.

View Replies View Related

Static Method Accessing Non Static Method

Oct 6, 2014

class Test
{
static Test ref;
String[] arguments;
public void func(String[] args)
{
ref.arguments = args;

[code]...

This code run perfectly.But the static method is acessing a non static method which was supposed to be not possible.

View Replies View Related

Accessing One Class File From A Fragment Or A MainActivity File

Dec 4, 2014

I have several different PopupWindows that i want to access from the same fragment. I would like for each PopupWindow to be it's own java file. Here is trimmed down code from an example showPopup.java file,

showPopup.java
public class showPopup {
//public class showPopup extends Activity implements View.OnClickListener{
//public class showPopup extends Window {
public static Context appContext;
PopupWindow popupWindow;
SQLiteDatabase db;
EditText edSpeciesLookup,edSpeciesLookupRowid;

[code]...

How do I configure inheritance to allow showPopup() class to reside in it's own Java file and be called from another class (file)? I've reviewed several hours of online resources as well as my reference books with no productive illumination. I've tried a number of possible implementations and extensions such as "public class showPopup extends Activity implements View.OnClickListener{...", and "public class showPopup extends Window.

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

How To Call A Method That Exist Within A Class Into Main Method

Feb 13, 2014

I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?

public class locker { 
public static void main(String[] args) {
CombinationLock();

[code]....

View Replies View Related

Calling Private Method To Another Method Located In Same Class

Oct 23, 2014

I am trying to call a private method to another method that are located in the same class.

I am trying to call prepareString to the encode method below.

Java Code:

public class ShiftEncoderDecoder
private String prepareString(String plainText)
{
String preparedString = "";
for(int i = 0 ; i < plainText.length();i++)
if(Character.isAlphabetic(plainText.charAt(i)))

[Code] .....

View Replies View Related

Dice Game - Invoking Method In Other Method Of Same Class

Feb 26, 2015

I am currently working on a dice game. I have a private method called rollDice and it performs the action of rolling two dice. For my project, I need to create another method called playerRolls and I am supposed to invoke the rollDice method in the playerRolls method and perform actions based off of that. My question right now is how do I invoke a method into another method of the same class?

View Replies View Related

Calling Method Of Another Class Which Is Implemented By Runnable Class

Aug 11, 2014

i am having a problem while calling a method..i am having a class

Java Code:

public class MySer implements Runnable {
public void getMessage(String msg)
{
...,
}..,
} mh_sh_highlight_all('java');
i use the above class in another class

[code]....

View Replies View Related

Creating Object Of Class And Calling Its Method In Different Class

May 20, 2015

In the process of creating a new class, I need to move my main method from the class SaveDate to the class DynamicTest. Below I have listed the code of both classes.The objective is to be able to run my program from the DynamicTest Class. I need understanding the process of moving my main method to a different class and creating an Object of a class and calling its method.
 
public class SaveData { 
  private static final Map<String, Object> myCachedTreeMap = new TreeMap<String, Object>();
   public static final List<String> getLines(final String resourceParam, final Charset charset) throws IOException{
  System.out.println("Please get: "+resourceParam);
  if (myCachedTreeMap.containsKey(resourceParam) ) {
  // Use the cached file, to prevent an additional read.

[Code] ......

View Replies View Related

How To Import Class Into Class With Main Method

Mar 3, 2014

Im writing a simple program to understand classes and objects. Basically what I have is a file called Program.java where I have my main method.I have another file called Person.java which I want to use to create Person objects. That person can have a name, email adress, phone number, etc.I put both these files in the same folder.in Program.java my first statement is:

Java Code: import Person.java mh_sh_highlight_all('java');

My problem is that when I compile Program.java i get an error message saying that the package Person.java does not exist.So my question is, when you create a class that you want to use for objects, how do you import that class into your class with the main method so that you can use instances of your other class?

View Replies View Related

Calling Method Of A Class From Main Class?

Aug 31, 2014

// Add range to Vehicle.
class Vehicle {
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon

// Display the range.
void range() {
System.out.println("Range is " + fuelcap * mpg);

[Code] ....

I'm compiling it in Eclipse and this continues to show in the console display

Minivan can carry 7. Exception in thread "main" java.lang.NoSuchMethodError: Vehicle.range()V
at AddMeth.main(AddMeth.java:34)

View Replies View Related

Using A Method From A Different Class?

Oct 25, 2014

I have a program that reads some text, and draws or deletes objects on a JFrame according to what I say. My program works fine for the most part. So whenever I say something like makeStuff(star) in my console, a star is drawn on screen. I kept track of types of objects in an ArrayList, and add and remove certain objects depending on the type of text I input. My problem is when I get to the point when there are no more objects of the same type on screen, meaning I keep clearing objects of the same type until there are no more, when I go to draw another object of the same type, it does not draw the object.

I have two classes, one for my console and one for graphical objects to be drawn. Strangely enough if I call clear twice then I can draw which doesn't make a lot of sense. When going through the debugging everything is working right, including the counters I made, and the amount of objects of the same type have the right amount when they are supposed to. I did find a work around by simply making the object in the console program class instead of calling the draw method from the sandbox class but my console class is probably a 1000 lines of code and is far too big already. I just want to use the draw method from the other class. Here are some revelant lines of code as to what I am trying to do:

Console Class
Java Code:

if (cmd.equals("clear") && arg.equals("star")) { //use a boolean for when this part is done
if (starCounter > 0) {
box.undoStar();
starCounter -= 1;
starNum -= box.getWidth() / 6;

[code]....

View Replies View Related

How To Get To A Method Of Inner Class

Feb 2, 2014

Imagine I have a class called Outer that contains a Inner class with a method.

Now there is a second class which wants to call that method:

Java Code: public class Outer {
public Outer() {
Inner inner = new Inner();

[Code].....

View Replies View Related

Method Name Same As Class Name?

Apr 3, 2015

I can't get errors of this code I think the problem is that the first method name is the same as the class name and was wondering what the simplest way to fix that is.

class CreditCard{
int transactionNum;
String merchantName;
double charge;
public void CreditCardMethod(int someTransactionNum, String someMerchantName, double someCharge){
 
[code]...

View Replies View Related

Linking A Method From Another Class?

Jun 16, 2014

I use the BlueJ software to develop my programs. I am creating a program which holds two data structures; Array List and Binary Tree. I have created a person class which is used to declare variables containing people, this class also contains a method which prompts the user to input 3 characters of a memorable word. These people are created within the array list and binary tree. This actually works in practice, within the program, but i get an error, and i need the program to contain no errors.

In my binary tree class, i want to call this method.I have attached 4 images to this post, an overview of the classes used, a look at highlighted code which is an attempt at calling a method from the person class, and the method contained within the person class itself. Note that the person class is just called "Person". The 4th image is the error being displayed.

[URL]

View Replies View Related

Method Overridden By Class

Sep 4, 2014

I would like to know what does it mean when a method is overridden by a class??Like for example when you append an object value to StringBuilder

View Replies View Related







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