Why create "new Object()"

Why would anyone want to create a Object?
Object obj = new Object();
I've seen people use it during synchronization locks but why create a new object instead of any other alternatives?
synchronized(obj) {
Are there any other uses for Object?

This is one of the most usual use for new Object() that I've come across - it's used to provide something to lock against that can be shared between various instances. An Object instance is considered lightweight (ie, the minimal thing you can lock against) and it doesn't imply that it will be used in your code for anything else (since it doesn't do much else).
You're right that there are other alternatives (often overlooked), such as synchronising on a Class instance (this.getClass() or MyClass.class) - this is at least guaranteed to exist and therefore its being lightweight doesn't really matter. But it's not always appropriate, especially if you've got various levels of locking going on. As usual, you've got to choose what best suits your own situation and your own personal style.
Another use for an Object instance is to provide an enumeration of constants whose value isn't important:
public class MyLayoutManager implements LayoutManager
  public static final Object NORTH = new Object();
  public static final Object SOUTH = new Object();
}I'm not suggesting that my example is ideal (personally I'd choose a constant whose toString at least made sense for debugging) but it does demonstrate how you can use an Object to provide constants for use by other bits of code (or even internally, say as keys into a Map) - they simply do something like MyLayoutManager.NORTH to indicate something to my code.
Clearly there are other uses for Object (such as being the base class for everything) and providing default implementations of hashCode, equals and all the locking stuff. But I can't think of any other uses of an Object instance that I've had before. No doubt other people have come across some...
Hope this helps.

Similar Messages

  • What is the diffrence between extends and creating new object?

    HI ALL,
    what is the diffrence between extends and creating new object?
    meaning
    class base{
    class derived extends base{
    class base{
    class derived {
    derived(){
    base var = new base();
    can u people tell me diffence from the above examples.
    THANKS.
    ANANDA

    When you create a new object you have to supply the class to which that
    object belongs. A class can extend from another class. If it does so
    explicitly you can define the 'parent' class from which the class extends.
    If you don't explicitly mention anything, the class will implicitly extend
    from the absolute base class named 'Object'.
    Your example is a bit convoluted: when you create a Derived object,
    its constructor creates another object, i.e. an object from the class from
    which the Derived class extends.
    Extending from a class and creating an object don't have much in common.
    kind regards,
    Jos

  • Problem in creating new objects in ABAP workbench

    Hi ,
        I am getting error in a newly installed SAP system when try to create new objects in ABAP workbench. The error is like'No changes for the user allowed'.
    Can you tell me what the error is here? What can be done for this so that I can create new object.
    Thanks,
    Manas

    Hi,
    Run tcode SU01. (Provided if u hav authorization to run this Tcode)
    Enter SAP* as user and click on the Copy button.
    Enter the name for your new user in the dialog box and click Continue
    Enter a password for your new user.
    Goto Profile sheet and adjust the profiles to be SAP_ALL and SAP_NEW.
    Save the user and logon with it.
    Now you'll be able to do your changes.
    Reward Points if useful.
    Regards
    Rose
    Edited by: Rose Preethi on Mar 31, 2008 5:20 PM

  • How to create new object key in BDS?

    Hello,
    I need to create custom class in BDS (OT type) and object key for it.
    In this forum (and in the whole Internet) I found information only about custom class. However there's nothing about object key.
    How could I create new object key in BDS class?
    Great thanx in advance!

    Hi Max,
    you can create BOR object in business object repository, Transaction SWO1, SWO2, search documentaion for BOR obkect.
    Regards,
    Clemens

  • Resuse existing object (Sp,tables etc)or create new object if that is possible?

    Hello Everyone,
    Is it a good practice to create new objects (stored procedures, tables etc.) or modify and reuse the existing object if that can be done?
    Regards
    Regards

    If you need simple add a new column to the SELECT statement so there is no need to create a new stored procedure/view for this
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Problem when creating new Objects in ESR

    Hi ,
    When i am trying to create new objects in ESR it is no allowing me to do,i have developed around 80 interfaces as of nowmsuddenly this eeror i getting,anyone experienced this kind of issue.
    *You cannot create new objects since the system is not the original system of the software component version*
    You cannot create new objects since the system is not
    the original system of the software component version (NOT_ORIGINAL_NO_CREATE)
    MESSAGE ID: com.sap.aii.ib.server.oa.rb_all.NOT_ORIGINAL_NO_CREATE
    Regards,
    Raj

    Hi Raja,
    I am new to SAP PI. So i dunno much regarding SAP PI. Hence i am getting same error that not able to create objects in SWCV. I read this discussion but i dunno where to select the 'Modifiable & Original' options, so that i can enable the Objects. Can you please guide me?
    Thanks,
    Amarnath

  • Why can't I create new object from a class that is in my big class?

    I mean:
    class A
    B x = new B;
    class B
    and how can I solve it if I still want to create an object from B class.
    Thank you very much :)

    public class ItWorksNow  {
      public ItWorksNow() {
      public static void main ( String[] argv )  throws Exception {
        ItWorksNow.DaInnaClass id = new ItWorksNow().new DaInnaClass();
      public class DaInnaClass {
    }

  • Create new object (measure) with IIF function on BW Olap universe:mdx error

    hello
    i have a universe based on a BEX query
    i want to create a new object (a measure) with the IIF(,,) function
    i created the following select:
    <EXPRESSION>IIF(@Select(Currency Type\CurrencyTypeCode)="10",@Select(Key Figures\RevenueGlobalCurrency),@Select(Key Figures\RevenueLocalCurrency))</EXPRESSION>
    defined as number
    where
    CurrencyTypeCode is a dimension (character format)
    RevenueGlobalCurrency and RevenueLocalCurrency are measure (number format)
    i always have a parse which is OK but
    when i want to  use the object on a webi report i always have an MDX error : ...invalid MDX with .... (WIS 10901)
    and advise ?
    Thanks

    Hi Didier,
    Thanks a lot for your help.
    Now it works : i do not understand why
    i explain:
    the correct syntaxe of my new measure is :
    <EXPRESSION>([Measures].[4FVLHG7OAJMNY9WC06X6JLOZQ], [0CURTYPE].[10])</EXPRESSION>
    it does not works with my "L01 currency type" BO dimension
    ([0CURTYPE].[LEVEL01])
    when i had :
    -my universe is based on a BW BEX query
    -my universe has been automaticaly generated with the assistant
    -the Currency Type Class was define as below:
           - 1 BO dimension object : L01 Currency Type =>
    [0CURTYPE].[LEVEL01]
           - 3 BO detail objects : L01 Currency Type techName =>
    [0CURTYPE].[LEVEL01].[TECH_NAME]
                                              L01 Currency Type Key =>
    [0CURTYPE].[LEVEL01].[[20CURTYPE]].[Value]
                                             L01 Currency Type Long Name =>
    [0CURTYPE].[LEVEL01].[[40CURTYPE]].[Value]
    i always had a wrong mdx syntax error on the webi report
    i updated the class with the follow
         - 1 BO dimension object : L01 Currency Type =>
    [0CURTYPE].[LEVEL01] (idem)
           - 3 BO detail objects : L01 Currency Type techName =>
    [0CURTYPE].[LEVEL01].[TECH_NAME]  (idem)
                                              L01 Currency Type Key =>
    [0CURTYPE].[LEVEL01].[NAME] (updated)
                                             L01 Currency Type Long Name =>
    [0CURTYPE].[LEVEL01].[DESCRIPTION] (updated)
    then it was working
    Then i tried to reproduce from scratch and now it also works with the previous version of the class.
    I do not understand anything
    Is there something like a cache file of the MDX query (in RSRT, cache mode is set to inactive for the bex query) ?
    ps: this is the same if a use webi under infoview (java) or webi rich client...
    Edited by: Frederic Nez on Jan 19, 2010 6:35 PM
    (edit : there is mistake in the post : objects are always between [] even if it is not dispay => change objects to code mode)

  • PDR Steps to create new object types

    We set a demo instance of the PDR tool here and it all works well.. I would now like to add purchase info records as an object that can be sequenced with all other master data like materials, material boms, variant class and ecms in a config folder. I know that new function modules would need be created but was wondering there was a document with steps expaining the entire procedure for doing this or if any other company has yet tried this with the tool.. It allows for the creation of new objects for the packet but would like to have some sort of guideline to follow if possible.

    Hello Mr Thorne,
    I knew there are some SAP Consultants that know how to implement a new object type. And there is a project together with the german army that expands the PDR with several object types.
    But you have to pay attention. The PDR consists of two main parts . The FOX - Frame work of Explosion and the UPS - Uniform Packaging Service.
    Creating a new Object type that can be "shipped" has to be in the UPS - BUT - if you are using the transaction CRWBD that new object type will not be found if you are exploding a baseline. That is the task of the FOX.
    And to add a new Object type into the FOX would be harder than add it into the UPS.
    I think the Consulting solution, uses a FOX exit to add that object into the UPS after the FOX-Run.
    I you need further information please contact SAP consulting (PLM).

  • Problems to create new object!

    Hello,
    I have a problem to create a new object in OracleLite 9i.
    I'm using okapi to do this, but the value of the numeric fields isn't the expected.
    In msql the field is number(10,0). I'm trying to insert a ok4B.
    Anybody can help me?

    I found the problem.
    It occurs when I use a number(x,y), where x &gt; 9 or Y &gt; 0.
    I made a workaround to solve this problem. This is simple, I just convert the field number to char in the Snapshot of the Wtgpack

  • Can creating new object without object reference cause OutOfMemoryException

    I am getting OutOfMemoryException in my application. After looking at the logs and doing some analysis I think creating a new object and not attaching it to a reference is causing the issue.
    The simplified code is as below:
    void valuate(int tradeNum){
    new CaptureTrade().process(tradeNum); //1
    Will the above code called n number of times cause OutOfMemoryException?
    Should I use something like this instead:
    void valuate(int tradeNum){
    CaptureTrade ct = new CaptureTrade();
    ct.process(tradeNum); //2
    Can the first program cause OutOfMemoryException which can be rectified using the second piece of code?

    ashay wrote:
    I am getting OutOfMemoryException in my application. After looking at the logs and doing some analysis I think creating a new object and not attaching it to a reference is causing the issue.
    The simplified code is as below:
    void valuate(int tradeNum){
    new CaptureTrade().process(tradeNum); //1
    Will the above code called n number of times cause OutOfMemoryException?
    Should I use something like this instead:
    void valuate(int tradeNum){
    CaptureTrade ct = new CaptureTrade();
    ct.process(tradeNum); //2
    Can the first program cause OutOfMemoryException which can be rectified using the second piece of code?What happened when you tried it?

  • Question about creating new objects

    When creating a DocumentBuilderFactory Object, why is the code written like this:
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();as opposed to
    DocumentBuilderFactory docBuilderFactory = new DocumentBuilderFactory();???

    Factory classes like this are typically abstract classes which do little more than define an interface and the getInstance() methods. The class that getInstance() will actually instanciate will be a concrete implementation from a particular implementation of the DOM parser. This pattern allows whatever implementation happens to have been supplied to be automatically be used without your code having to concern itself.
    The concrete factory will then create concrete implementations of the various interfaces like DOMPaser, using the implementations in the actual library.

  • Generics and creating new objects

    Assume this class:
    class Yatta<T>
      public Yatta()
        T t = new T();
    }When compiling I get "unexpected type" refering to the "T" in "new T()". Why isn't this allowed? Is there any way of doing something similar without creating a huge chain of factories for each type?

    Please use single letters for type parameters. Doing what you've done with "Type" makes reading the code difficult...
    Since duplicate() is declared in Component I assume it is declared to return Component. How can the compiler possibly know that it is going to return a more specific type? It can't. (Well... you can tell it... see below)
    Because of what I wrote in the first paragraph, it's not immediately obvious that Type is a type parameter, but it is, and casting to a type parameter doesn't get you anywhere. From this code and your original post, you appear to be treating Java Generics like C++ templates. They are not the same. In particular, Generics are implemented using a technique called erasure which means that the type parameter information is not present in the compiled class files. The practical upshot of this is that the cast in the line you highlight is in fact a cast to Component (since that is the upper bound of Type) and this cast is useless because we already know the c is a Component. So the compiler is correctly warning you that the typecast will not actually perform the check that you want it to.
    (In fact, if you try this in Eclipse, you get a better warning: "Type safety: The cast from Component to T is actually checking against the erased type Component")
    So... how to fix this...?
    Well, you need to make the return type of duplicate() change depending on the concrete implementation. You could do this by parameterising Component thus:
      public interface Component<T extends Component<T>> {
        public T duplicate();
      }Now, every use of Component will need to be parameterised. In the case of the code you have below, you'll need:
    public class Container<T extends Component<T>> extends ArrayList<T> {
      public Container() {
        //Empty
        // Why write it then??? Get rid of it.
      public Container(Container<T> object) {
        Iterator<T> i = object.iterator();  // don't need ListIterator here
        while(i.hasNext()) {
          add(i.next().duplicate());
    }and every implementation of Component needs to look like this:
    public static class Something implements Component<Something> {
      public Something duplicate() {  // note the return type
        return null;  // or something more useful!
    }Hope that helps.
    (PS 'tabs' are the work of satan and should never be found in source code)

  • Unable to create New objects in SE37 & SE38

    Hi Experts,
    I am ABAPer and facing belowing mentioned problem.
    I am unable to create any custom objects like executable programs/BAPI/FM due to performance.
    I am able to open required transactions like SE37/SE38, after that If I try to create or test object, the system is not responding, again we need to go to task bar to close the session.
    I tried to debug SE38 to know what happening internally, it is closing automatically.
    I tied the below mentioned steps .
    1).  I am able to Creating/ Deleting Tables in SE11.
          Unable to create executable program.
           Open SE38 àEnter z----- object name à Click on Create à in new window  Provide Description àSelect program type u201CExecutableu201D à in new dialogue select package &  
           click to save the object. (Here is the problem , we need to restart session, unable to save object properly)
    2).  Function module and BAPI  I am trying to test in SE37.
           However I am able to open se37 transaction with in few seconds,
          After entering FM/BAPI name in SE37, if we click on Display or try to execute  that FM/BAPI system is not responding.
           Open SE37 à Enter existed object name example RFC_READ_TABLE à click on Display   (System will not respond, no content will be displayed)
    As per my knowledge, when ever interacting with SAP Database  system is going to struct,
    However a basis Consultant can understand better then me.
    Please suggest and let me know what need to be done further.
    Thanks & Regards,
    SAI

    Hi,
    I had that also, the window war frozen.
    The problem was the editor mode.
    => go to poperties.
    => change to mode to "old editor". don't use the first option (Now, I don't know the description). "Editor (new)"
    (a bug of the frontend or the backend)
    Sven

  • Problem in creating new object in this code below

    private Player owner;
    private int armies;
    public Territory(String name, Continent continent)
    owner = new Player(Territory territory, Player owner); <============ ) is expected
    I have an error following that code. It said that ")" is expected

    our main error was territory class requires a continent object not an territory object
    public class Continent
    private String name;
    private Territory territory1;
    private Territory territory2;
    public Continent(String name)
    this.name = name;
    territory1 = new Territory("Territory1", new Continent("Asia") ); // instead of territory1 use new Continent("Asia")
    territory2 = new Territory(); // GIVE IT TWO PARAMETERS LIKE "territory 2" and new Continent("Asia")
    Oh my bad, I means the territory2 = new Territory("Territory2", new Continent("Asia"); // it is same :D
    but when I want to create territoty for Europe, I will do a repeating code like this in class Territory n our lecturer forbid it:
    territory1 = new Territory("Territory1", new Continent("Asia")); //Pass an object as argument
    territory2 = new Territory("Territory2", new Continent("Asia"));
    territory1 = new Territory("Territory1", new Continent("Europe")); //Pass an object as argument
    territory2 = new Territory("Territory2", new Continent("Europe"));
    is there any other way to do it in single code? such as bring the objects in class World together
    private Continent asia;
    private Continent europe;
    Continent asia = new Continent("Asia");
    Continent europe = new Continent("Europe");
    instead using new Continent("Asia") we use the object in class World pass it to class Continent and then from Continent pass them to Territory
    like this:
    territory1 = new Territory("Territory1", new Continent(name)); //Pass an object as argument
    territory2 = new Territory("Territory2", new Continent(name));
    since we has variable called String name, so we can just pass it whatever that is passed from class World
    public class Continent
    private String name;
    private Territory territory1;
    private Territory territory2;
    public Continent(String name)
    Thanks a lot in helping us, have a nice day.

Maybe you are looking for

  • Can I return my iMac to snow leopard

    Can I return my iMac from Lion to Snow Leopard?

  • How to use 27.x securities in 3.x

    As everybody knows from Firefox 4.x to 27.x uses much more RAM than firefox 3.x. Mine questions are as follows 1. Does firefox team provide a lite version which will use less RAM (approx. 64 MB- 128 MB). 2. If not Than how do i use 27.x securities an

  • Computer/Macbook can't detect Iphone

    I've been trying to connect my iphone to my macbook but it just would'nt be detected. I have tried different cables and computers but my iphone is still undetectable. It pretty clear that its my phone that has an issue rather than my computer. Also,

  • Best way to synchronize in a Singleton

    I have this classimport java.util.ArrayList; import java.util.List; public class SynchIssues   private List values = new ArrayList();   private static final SynchIssues singleton = new SynchIssues();   public static SynchIssues getInstance()     retu

  • HT1694 hotmail account Sync problems with my iMAc . H

    Hi really strange problem, i receive hotmail account emails wit "Mail" but whatever action i do on it , like read , delete etc.. doesnt reflect on Hotmail Server ( hotmail web page) That problems is not reflected on my iphone or ipad. Thnks Rubén.