Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Beginners question (http://www.go4expert.com/forums/beginners-question-t26235/)

askmurphy 8Jul2011 00:12

Beginners question
 
Hello !
I need some help with the following:

I have 2 java-files (classes), in the 1st one I want to declare a class with some variables. These variables must be initialised with some values. After that, I want to call (from the 1st class) the class in the 2nd file which needs the variables from the first class.

Because I want to use a block of variables containing e.g. 25 variabels, I want to parse just a reference or something to the 2nd class.

How can I do this ?

I hope somebody want to show how this can be done :)

virxen 8Jul2011 13:59

Re: Beginners question
 
you can try something like this


class that uses the variables of the other class
Code:

public class TestMath{
    public TestMath(){
    MathVirxen math=new MathVirxen();
    System.out.println("sin30+cos60="+math.sin30+"+"+math.cos60+"="+(math.sin30+math.cos60));   
    }
    public static void main(String args[]){
        new TestMath();
    }
}


class with the variables you want
Code:

public class MathVirxen{
    public double PI=3.1415926;
    public double sin30=0.5;
    public double cos60=0.5;
   
}


askmurphy 8Jul2011 14:29

Re: Beginners question
 
Ok, great ;-)

But now I want to use this same datablok-variable in a 3th file, how can I do that ?

Thanks for your help !!!

ManzZup 8Jul2011 21:09

Re: Beginners question
 
no problem because as virex clearly shows, you can access thing from any other java file

let me explain the code you seem not to grab the concept
[i did a bit of modification becuase i think making a new instance is not necessary for the purpose, any way both codes will work]

class with variables you want or the your first class file:

Code:

//note that the class is public, so this class is visible to all other classes
public class MathVirxen{
    public static final double PI=3.1415926;
    public static final double sin30=0.5;
    public static final double cos60=0.5;

    //Note that all the variables are also declared as public, so any class outside see and can access these
  // I made them all static so then an instance of the class is not necessary for another class to access the variables
  // the final modifier is not a must but depending on the situation, you would like the values to be constant or you can accidently change there values 
}

to access from any other class
Code:

Class OtherClass{
    public static void main(String... args){
              System.out.println(MathVirxen.PI);
    }
}


askmurphy 9Jul2011 00:19

Re: Beginners question
 
Ok, but I would like to have the datablock private, not public.
So, it would be nice if I could get the datablock in a 3th class, ofcourse pointing to the datablock
from the 1st class above.

I have tried it with something like this in the first class:

Public MathVirxen getdatablock()
{
return(math);
}

But the compiler (netbeans) don't accept that..

Any help ? Many thanks anyway !!!

ManzZup 9Jul2011 19:15

Re: Beginners question
 
Code:


public class MathVirxen{
    private static final double PI=3.1415926;
    private static final double sin30=0.5;
    private static final double cos60=0.5;

    public static double getPi(){
            return PI;
    }
}


Code:


Class OtherClass{
    public static void main(String... args){
              System.out.println(MathVirxen.getPi());
    }
}

hope this is what you trying to do as your above is not very clear to me

askmurphy 10Jul2011 19:28

Re: Beginners question
 
Thank you all, this works ;-)

BUT: what if I want to re-use the main class (for example, multiple calls to open a specific class which opens a window and containing a variable-block).

In that case, I can't use public variables, because I need different variables for every window...

I tried that (private and not static instead of public static), but that gives compiler errors...

Anyone ??

Thanks again everybody, you are a great help ;-)

ManzZup 10Jul2011 20:59

Re: Beginners question
 
then donot make them static because static means they are not instance related but it is related to the class
i think this would be something you are looking for

Code:

public class Reuser{
      public int someNum = 10;
      private String name;
      public double sum;
   
      //you dont have static ones anymore because static ones are not changedd with instance
    //you can use both public and private and the difference is that you cannot directly call the private ones, i'll show it in the other code block

      public Reuser(String str){
            name = str;
      }
      //This is a class constructor, as i cannot directly assign value to the private variable, i use the constructor to do that and even you can use a setter method to do this
      public void setName(String str){
          name = str;
      }
 
      //To get the value of the method you use a getter
      public String getName(){
            return name;
      }
 }

A class that uses multiple of this

Code:

class User{
        public static void main(String... args){
                Reuser user1 = new Reuser("Giving the name");
                Reuser user2 = new Reuser("You always have to set this as there's no defualt constructor now");
                user1.setName("Changing the name");
                System.out.println(user1.someNum);  //calling a public variable no prob;
                user1.sum = 1221.31361d; //setting a public variable so no prob
                System.out.println(user1.sum);

                //The below code IS COMMENTED BECAUSE IT IS WRONG
                // user1.name = "You cannot access private variable like this";
              // System.out.println("Not even like this :" + user1.name);

                System.out.println(user1.getName()); //you can use the getter method to recieve the value
 
        }
}

I hpe you get that, but please tell me for any erros in the code because i didnt compile the code beforehand ;)

askmurphy 20Jul2011 04:36

Re: Beginners question
 
Hello !
I have done this now:

----------- DataStorage.java --- start ---
Code:

package mytest;

public class DataStorage {
    public double m1,m2;
    public String massa1;
         
  public static DataStorage createDatablok() {
        DataStorage datablok = new DataStorage();
        return datablok;
    }     
}

----------- DataStorage.java --- end ---


----------- OpBox.java --- start ---
Code:

package mytest;

public class OpBox extends javax.swing.JDialog {
    public static DataStorage datablok = DataStorage.createDatablok();
               
    public  static DataStorage GetDatarecord() {
      return(datablok);
    } 
   
    // some more code here for the Dialog, not importent for this sample..

}

----------- OpBox.java --- end ---

In some other java-files I use this:

DataStorage drec = OpBox.GetDatarecord();
drec.massa1 = "2000";
// etc...

----------------------
So far so good, works perfect !
Maybe I do something wrong, check the above things and if it's wrong, please tell us what's wrong
and how it must be fixed ;-)


BUT: I have 2 problems now:

1) Everything works ok when I build and RUN the program inside NetBeans.
But it starts a bit slow, not sure why ? Also parts of the program are a bit slow the first time, for
example if I open a new dialog and close it and open it again. First time it's slow, next times it's fast.

2) When I RUN the builded RUNTIME version (myprogram.jar) on a Windows 7 (32bit, 3Ghz) computer,
so not inside the NetBeans environment, it works but a part of the program doesn't work at all.
(some selection to open a new Dialog, it doesn't open the new Dialog).
Don't understand how this is possible ?

Any suggestions ????

Many thanks in advance !!!

ManzZup 20Jul2011 20:30

Re: Beginners question
 
i cannot tell anything for sure without seeing the complete thing
as there can be various reason onto you issues

but i see something in the code above, i think it can have a problem with memory [but i can be wrong]

public static DataStorage createDatablok() {
DataStorage datablok = new DataStorage();
return datablok;
}

here you create the datablok object inside the function and return it, usually returning local members like objects is not bad, because once the function is over usually the things created with in the function are eligible for gc()
but in this case they will be tied again for a reference, so for me the situation is a bit tricky

better if you define the datablok out of the boudary and return it from the function


All times are GMT +5.5. The time now is 21:25.