6 Replies - 156 Views - Last Post: 07 February 2012 - 09:49 PM Rate Topic: -----

Topic Sponsor:

#1 vibinvictoria  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 76
  • Joined: 16-July 08

How to create shared object?

Posted 07 February 2012 - 02:27 AM

Hi Experts,

How can i create shared objects in java.. please share any url ref. or any sample code .. Thanks in advance,
Is This A Good Question/Topic? 0
  • +

Replies To: How to create shared object?

#2 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: How to create shared object?

Posted 07 February 2012 - 02:33 AM

You can use the 'static' keyword. So then you can access it using ClassName.fieldName. You can't declare a class with a static modifier, so it must be a field (or method). If you only want one instance of a class at any time, you can look into a 'Singleton'. (I don't know anything about those, so I can't explain).

Edit: To prevent confusion, you can declare inner classes static, but not the main class of any particular file.

This post has been edited by Mylo: 07 February 2012 - 02:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 vibinvictoria  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 76
  • Joined: 16-July 08

Re: How to create shared object?

Posted 07 February 2012 - 02:49 AM

View PostMylo, on 07 February 2012 - 02:33 AM, said:

You can use the 'static' keyword. So then you can access it using ClassName.fieldName. You can't declare a class with a static modifier, so it must be a field (or method). If you only want one instance of a class at any time, you can look into a 'Singleton'. (I don't know anything about those, so I can't explain).

Edit: To prevent confusion, you can declare inner classes static, but not the main class of any particular file.


Hi Mylo,

Thanks for ur kind information.. can we say class level variable/methods are shared object? please share ur suggestion..

thanks in advance
Was This Post Helpful? 0
  • +
  • -

#4 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: How to create shared object?

Posted 07 February 2012 - 02:54 AM

I'm quite new to java myself so I'm not familiar with many of the terms thrown around. I have never heard 'shared object' before so it seems something like 'static field' or 'static member' is preferred, those I have heard frequently. I think people will know what you are talking about anyway. (It could have different meaning in other languages though)
Was This Post Helpful? 0
  • +
  • -

#5 vibinvictoria  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 76
  • Joined: 16-July 08

Re: How to create shared object?

Posted 07 February 2012 - 02:59 AM

Thanks Mylo.. will wait for experts answer
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: How to create shared object?

Posted 07 February 2012 - 04:28 AM

Any static class member is shared between all instances of a class. That means if you have a static member in a class(either variable or method) then we have only one copy of it no matter how many objects of that class we have. Different from instance variables and methods where each object/instance takes its own copy of it.
So for example I have a class:
public class CSStudents{
    //instance variable
    private String name;
    //static/ shared variable
    private static int totalYear;
    //...
}

Then I create two objects:
CSStudents std1 = new CSStudents();
CSStudents std2 = new CSStudents();


These two students every one will have its own copy of name(every one will have his/her own name) but the shared copy of totalYear. Means if totalYear is set to 3, then all CS students will have a total of three years to complete studies.
That is the idea of static variables and methods, only one copy is shared between all instances and one change affects all objects.

That is why we reference static variables using class name and not object name. For example if in the above class I have a shared method below:
public static void setTotal(int x){
    totalYear = x;
}

This is a method to change the total years of study for CS student class. then to call it I say:
CSStudents.setTotal(4);

Now I have changed all CS students completion years to 4.

It is always advised not to have many of static members since they remove the testy of OO, so try the best to avoid them. But sometimes they must be there, for the situation where you need all objects to share something like the example above, or in BankAccount class, people with the same account type have the same interest rate always, so it should be static.

Quote

can we say class level variable/methods are shared object? please share ur suggestion..

Not all variables are objects, some of them are primitives, and methods are not objects, so cant call them "shared objects". But if you have static object anywhere, then you can call it so.
The java doc said:

Quote

If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4).

A field that is not declared static (sometimes called a non-static field) is called an instance variable. Whenever a new instance of a class is created, a new variable associated with that instance is created for every instance variable declared in that class or any of its superclasses.

Was This Post Helpful? 2
  • +
  • -

#7 vibinvictoria  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 76
  • Joined: 16-July 08

Re: How to create shared object?

Posted 07 February 2012 - 09:49 PM

Hi smohd

Excellent smohd.. Really great.. now i can understand . THANKS A LOT
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1