send int to another class

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 3444 Views - Last Post: 17 May 2010 - 08:52 AM Rate Topic: -----

#1 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

send int to another class

Posted 13 May 2010 - 01:34 PM

I want to do somthing like this.

Class1
public class class1
{
	int number_to_send = 71;
	
	class2.set_number_holder(number_to_send);
}


Class2
public class class2
{
	int number_holder;
	
	public void set_number_holder(int num)
	{
		this.number_holder = num;
	}
}


But I cant do this because when I type "class2." my editor doesn't show me my method that I made in class2. I know if I created the Class2 variable in class1 it would work but I cant do that. I need to get that int to the other class without creating a class2 variable in class1.

This post has been edited by giuseppe105: 13 May 2010 - 01:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: send int to another class

#2 pmiller624  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 02-May 10

Re: send int to another class

Posted 13 May 2010 - 01:42 PM

You have to create an instance of class2 inside class1.
public class class1
{
        Class2 c2 = new Class2();
        int number_to_send = 71;
        
        c2.set_number_holder(number_to_send);
}



I don't think there is another way. why can't you create an instance?

This post has been edited by pmiller624: 13 May 2010 - 01:46 PM

Was This Post Helpful? 3
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 484
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: send int to another class

Posted 13 May 2010 - 01:46 PM

In order for you to invoke a method on the class directly instead of creating an instance of the class, and call it on that you need to make the method static.

Here's an example:
class A {
   public static void staticMethod(){}
   public void nonStaticMethod(){}
}


Driver class:
...
A obj = new A();

obj.staticMethod(); //OK
A.staticMethod();   //Also OK (preferred)

obj.nonStaticMethod(); //OK
A.nonStaticMethod(); //Doesn't work! nonStaticMethod isn't static!



Hopefully that'll give you an idea of what you need to do :)
Was This Post Helpful? 2
  • +
  • -

#4 benregn  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 17-November 09

Re: send int to another class

Posted 13 May 2010 - 01:56 PM

Isn't enough to have a reference to class2?

public class class1
{
    private Class2 class2;

        int number_to_send = 71;
        
        class2.set_number_holder(number_to_send);
}


This post has been edited by benregn: 13 May 2010 - 01:59 PM

Was This Post Helpful? 2
  • +
  • -

#5 pmiller624  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 02-May 10

Re: send int to another class

Posted 13 May 2010 - 01:57 PM

I was wrong in class2 extend it to class1..

public class class1 
{
        int number_to_send = 71;
}


public class class2 extends class1
{
        int number_holder = number_to_send ;
}

This post has been edited by pmiller624: 13 May 2010 - 01:59 PM

Was This Post Helpful? -1
  • +
  • -

#6 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

Re: send int to another class

Posted 13 May 2010 - 02:01 PM

Im sorry but that doesn't help because both of you told me to make a vairable with class2 inside class 1 and i cannot do that.

edit:
pmiller that might work let me try it

This post has been edited by giuseppe105: 13 May 2010 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#7 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

Re: send int to another class

Posted 13 May 2010 - 02:12 PM

Ok i cant do that because im already extending the class1 as a JPanel can i implement class2?
Was This Post Helpful? 0
  • +
  • -

#8 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

Re: send int to another class

Posted 13 May 2010 - 03:21 PM

Is there not some other way of doing this like using the clipboard or writing to memory?

all i need is to send a number from 1 class to the next one but cant make the second class a variable inside the first one and cant extend the second class in the first one ether.

is there another option like useing memory directly. or networking or clipboard control something that 2 different classes can access.
Was This Post Helpful? 0
  • +
  • -

#9 pmiller624  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 02-May 10

Re: send int to another class

Posted 13 May 2010 - 03:49 PM

The only other option I can think if is to use a scanner. With scanner you can write the variable to a file from class1 and then read the variable from class2.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

static final String filename="blahblahblah.txt";
static PrintWriter fileStream;
Scanner datafile;



Write to a file
       try{
           fileStream = new PrintWriter(new FileOutputStream(filename));
        }
       catch(FileNotFoundException error){
           System.exit(0);
        }
        fileStream.println(number_to_send);
        fileStream.close();



Read from file
        try{
           datafile = new Scanner(new FileInputStream(filename));
        }catch(IOException e){
            System.exit(0);
        }

        while(datafile.hasNextLine()){
             try{
            	  number_holder = Integer.parseInt(datafile.nextLine());
              }catch(Exception e){
            	  
              }
        }
        datafile.close();



Error catching is not completely done... it will need work

This post has been edited by pmiller624: 13 May 2010 - 03:51 PM

Was This Post Helpful? 1
  • +
  • -

#10 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 484
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: send int to another class

Posted 13 May 2010 - 03:57 PM

View Postgiuseppe105, on 13 May 2010 - 06:21 PM, said:

all i need is to send a number from 1 class to the next one but cant make the second class a variable inside the first one and cant extend the second class in the first one ether.


All you need to do is use a static method in the class you want to receive the int. See my previous post :)
Was This Post Helpful? 2
  • +
  • -

#11 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

Re: send int to another class

Posted 13 May 2010 - 04:51 PM

@eric.price

I don't think i can do what you said because in your explanation you said
A obj = new A();
and i didn't really understand much from your explain other then that static makes a static method... witch i already knew. so i don't really understand if i read what you said correctly could you help me understand it exactly?
Was This Post Helpful? 0
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1500
  • View blog
  • Posts: 3,211
  • Joined: 11-December 07

Re: send int to another class

Posted 13 May 2010 - 04:55 PM

Sending data between classes is a really standard thing in Java (and any other language with classes). You'll only end up overcomplicating it if you do something like using the clipboard or networking(!!!)

erik.price is telling you how to share static fields and pmiller's first post is telling you how to share data between instances. Both are standard methods and you should use one of them.

If you are subclassing JPanel you will have an instance. You should probably have an instance of class two stored in class 1. You can pass data like this:

public Class1 extends JPanel {

  private int intToShare;
  private Class2 dataReciever;

  public Class1(Class2 anyInstance) {
    intToShare = 10;
    dataReciever = anyInstance;
  }

  public void someMethod() {
    dataReciever.printStoredData();
    dataReciever.transferData(intToShare);
    dataReciever.printStoredData();
  }

}


public Class2 {

  private int someData;

  public Class2() {
    someData = -1;
  }

  public void transferData(int data) {
    someData = data;
  }

  public void printStoredData() {
    System.out.println(someData);
  }

}


Take a look at the code. If someMethod() is called, Class 2 prints the data already stored (-1), the new data is transferred over and class2 prints out the data now stored (10)

This post has been edited by cfoley: 13 May 2010 - 04:56 PM

Was This Post Helpful? 1
  • +
  • -

#13 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

Re: send int to another class

Posted 13 May 2010 - 05:20 PM

Class2 cannot be a subclass of class1. or I could have just created a class2 variable inside class1.

I got eric.prices example to work. the only problem is that the variable i need to use is static...

I dont know how static stuff works i avoid it since i always get error with it.

Is there a way to convert a static variable to a non static one?
Was This Post Helpful? 0
  • +
  • -

#14 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1500
  • View blog
  • Posts: 3,211
  • Joined: 11-December 07

Re: send int to another class

Posted 13 May 2010 - 05:41 PM

My example does not make class2 a subclass of class1. There is a huge difference between having a variable and subclasing. Subclassing would mean that class 1 is a class 2. While a variable means it has a class 2. I strongly suggest you don't use sublcassing in order to access variables. In a vew limited circumstanced it can work but it's not the way it is intended to work.

If the variable is static, eric.price's method is the way to go. What errors are you getting? If you post you8r code, we will be able to give you better advice.
Was This Post Helpful? 0
  • +
  • -

#15 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

Re: send int to another class

Posted 13 May 2010 - 05:46 PM

Class1
public class class1
{
	public static void main(String args[])
	{
		class2.set_number_holder("71");	
	}
}


Class2
public class class2
{
	static String number_holder;
	String new_holder;
		
	public static void set_number_holder(String num)
	{
		number_holder = num;
                new_holder = number_holder;
	}
}


I never use the work static in any of my programs.

the error i am getting is line 9: non-static variable new_holder cannot be referenced from a static context.

This post has been edited by giuseppe105: 13 May 2010 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3