   [java] Pass created class throught clasess
  I recently worked in C++ and i have no idea how to perform this operation in Java.Basically i want to achieve C/C++ like global variable.Situation:I have a class (i.e. Error Loging To File Class)In main.java:... ErrorLog log = new ErrorLog("mylogfile.log");...how do i pass whole "log" variable/class to other clasess i.e. renderer, filesystem, network.i dont want to create new "ErrorLog log = new ErrorLog("mylogfile.log");" at every of those clasess.i want to achieve this like effect:rendere.java... log.add("abc");...Is there any good solution besides special function in any of new classes (something like this): public void passErrorLog(ErrorLog pLog){ log = pLog; }p.s. I had no idea what keywords should i use to search about this topic on google.   
  You can make the log a public static member.   ;  Well, I will take the risk of beginning a flame war about design patterns... but perhaps you want a Singleton?public final class MyLogSystem {    private static MyLogSystem logSystem;  private static File logFile;  static {    logSystem = new MyLogSystem();  }  // Private constructor  private MyLogSystem() {    try { logFile = new File("logs.txt"); if (!logFile.exists())   logFile.createNewFile();    } catch (IOException ioex) { ioex.printStackTrace();    }  }    public static MyLogSystem getInstance() {    return logSystem;  }    public void writeOnLog(Exception e) {    // write on the log  }}// Then, you use it like this:Exception e;MyLogSystem.getInstance().writeOnLog(e);;  I feared that one day i will face "The Singleton" he he...Fantastic, your example is working perfectly. Im thinking if there is any way to make function names smaller, system.out.println("abc") = toConsole("abc");MyLogSystem.getInstance().writeOnLog(e); = toLog(e);   ;  You can either write these "wrapper" methods in the local classes, or write general purpose static methods in the singleton to get rid of the getInstance() method - but you would still have to call these methods by the class name.   ;  Have you tried Log4j?  Though I know you are not in the mood to create a log for all of your object or classes (which you don't with Log4j) but you can add a log to each of the classes you do want information from.  Besides that Log4j has a cool XML interface that lets you set the level of log reporting and for which Classes the reporting is done.Its free from apache.org.import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;//~--- classes ----------------------------------------------------------------public class SomeClass {    /** There is a logger in every class DO NOT REMOVE THIS LINE */    private static Log log = LogFactory.getLog(SomeClass.class);That's what's required in the classes.  And the XML file looks like this:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">  <appender name="console" class="org.apache.log4j.ConsoleAppender">    <!--<param name="Target" value="System.out"/> -->    <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p - %m (%F:%L) %n"/>    </layout>  </appender>    <category name="org.something.SomeClass">   <priority value="debug" />    </category>    <!-- The Root directory of your package structure -->    <category name="org.something">   <priority value="error" />    </category>    <root>   <priority value="off" />   <appender-ref ref="console" />    </root></log4j:configuration>There are a few levels.  "Info", "Degug", "Error" etc.  And you can control what gets appended, like the source code line number, class name, etc.  See the top of the XML.// In the code somewhere:if( /*Something bad is about to happen*/ ) {   log.error("Monkeys are about to attack...");}if( /*In case you already knew monkeys were about to attack*/ ) {   log.info("There are only 200 monkeys attacking");}if( /*In the case you want to find out how many monkeys are attacking*/ ) {   log.debug("Number of Monkeys attacking : " + monkeyCount);}This works great with Ant too by the way.  And if the XML file is only reporting errors, then the log will only output log.error()'s and ignore the rest.  Then if you have "debug" set it will report both "error" and "debug"...its heirarchial, so with "info" you get all three.L-   