I am new in Java universe. Back in my PHP days, I had a PHPFUNCTIONS.php file which contained collection of functions like:
<?php
// PHPFUNCTIONS.PHP................
Function XYZ (var1, var2,..)
{
do this and that with var1, var2
return val1;
}
//End XYZ function
Function ABC (var3, var4,..)
{
do that and this with var3, var4
return val2;
}
//End ABC function
...
..
?>
I could call any of these function from other .php files and pass vailables back and forth like:
<?php
// WORKTODO.PHP.....
include "PHPFUNCTIONS.php";
....
....
$A1B1=XYZ('abcd','ghij');
.....
<?
I have a hard time visualizing this concept in Java classes. How can I achive this in Java? Thanks
05-15-2015 4:67 pm
Okay. So, now I have:
package org.tests;
public class TestUtils {
public static void main(String[] args) {
double tempDegree=65.4;
String lowerCase="lowercase";
System.out.println("Fahrenheit=: "+ToFahrenheit(tempDegree));
System.out.println();
System.out.println("Upper=: "+ToUpper(lowerCase));
}
}
I get the following Error: TestUtils.java:6 error: cannot find symbol System.out.println(Fahrenheit=: "^ToFahrenheit
package org.tests;
public final class MyUtils {
private MyUtils() {}
public static Double ToFahrenheit(CelsiusTemp){
Double CelsiusTemp;
if (CelsiusTemp == null) {
return null;
}
return (CelsiusTemp)*9/5 + 32;
}
public static String ToUpper(LowerWord){
String LowerWord;
if (LowerWord == null) {
return null;
}
return LowerWord.toUpperCase();
}
}
I get the following Error: MyUtils.java:4: error: expected public static Double ToFahrenheit expected public static String ToUpper(LowerWord)^{
I am stuck... and not getting this concept.. any help?!
The common java idiom for such a usecase would be to have some utiliy class:
package org.something.myproject;
public final class MyProjectUtil { // Or a more meaningful name, hopefully {
private MyProjectUtil() {}
public static int abc (int a, int b...) {
// do stuff
return a;
}
}
and then use if from other classes. Possibly even use import static org.something.myproject.MyProjetUtil.abc;
so you can call abc
directly without prefixing it with MyProjectUtil.abc
.