Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-01-2009
Member
 
Join Date: Nov 2008
Posts: 1,185
The keyword "static", attributes and methods of class in OOP

Quote:
NOTE: This article explains the meaning of the keyword static in a class. The meaning of "static" in C for example has nothing to do with it.
You are starting in object-oriented programming (OOP). In Java, C++, ... or whatever, you have dropped the word "static" and you do not know why. You do not know how. You do not know where ... But yet it exists.

The cake recipe

Attribute Instance

Suppose that you are about to receive friends tonight. You reached an idea, for dessert, to prepare a cake. But the problem is that you do not know how. The dessert is not your cup of tea. You then decide to create a cake: the pancake. You open your cupboard and your fridge, and all you find is this:
  • Butter
  • Eggs
  • Of flour
  • Sugar
  • Carrots

3 hours later

This is the third pancake you try to do. Yet one that is flat, one that burned, and another that was not bad but you've eaten because you were too hungry.

Must redo one.

You are tired, you're a geek, not a pastry chef and then you want to program, not to make cakes. But you say that you could combine business with pleasure.

Why not model your recipe in Java to statistics: in which case it is too flat, in which case it is too bitter etc ...

So where is the cake, or is the recipe?

The recipe is what is constant, which is common to all your pancakes. That is to say that there is butter, flour, carrots and so on ....

The pancake, it is variable and can make full. It will be different.

So you say that the recipe is a class and cakes produced from this recipe are instances of this class objects.

So that's what happens:

Code:
class Recipe
{
      public int flour; 
      public int sugar; 
      public int eggs; 
      public int butter; 
      public int core; 

      public Recipe (int flour, int sugar, int eggs, int butter, int carrot) 
      {
          this.flour = flour; 
          this.sugar = sugar; 
          this.eggs = eggs; 
          this.butter = butter; 
          this.carrot = carrot; 
      }

      public void cook () 
      {
          .... 
      } 
}
Note that these variables are specific to each pancake you make. The mix of flour one is not necessarily the same as another. And if you put sugar a little later before cooking, this change will be reflected on the pancake on you added to flour, not on others.

Example:
Code:
  // It creates two pancake 
  Recipe a = new cake Recipe(100, 200, 3 120, 4); 
  Recipe b = new cake Recipe(100, 200, 1, 120, 5); 

  a.flour = 400; // On doing a good mash flour 

  System.out.println (a.flour); // Show 400 
  System.out.println (b.flour); // Show 100
As you can see a change was not reflected in b.

This is called a variable instance or attribute instance. It is specific to a single object, every object has its own copy.
Reply With Quote
  #2  
Old 05-01-2009
Member
 
Join Date: Nov 2008
Posts: 1,185
Re: The keyword "static", attributes and methods of class in OOP

Attribute class

You drop your code two seconds, and touched by grace you again, pancake completely successful this time. You love this cake. So you say you want us to let people know his name. But you are aware that languages change over time, the names too. If your cake is successful, it is likely that the revenue will go up in 2030, and people will forget it.

Are you facing a new problem: you want a variable "name" which has the same value for all your cakes. You would like if the name is changed, the change affects all cakes.

Ideally, frankly, would be to put the name on the recipe and you let anyone give the name he wants this cake

This type of variable, common to all instances of a class, and shared by all instances of a class called a class variable, or class attribute.

To set this variable, use the keyword "static". To access it, use the class name followed by a point and then the name of the attribute. It also works using the object name followed by a point and then attribute.

Example:

Code:
class Recipe 
{
      public static String name; 

      public int flour; 
      public int sugar; 
      public int eggs; 
      public int butter; 
      public int carrot; 

      public Recipe (int flour, int sugar, int eggs, int butter, int carrot) 
      {
          this.flour = flour; 
          this.sugar = sugar; 
          this.eggs = eggs; 
          this.butter = butter; 
          this.carrot = carrot; 
      }

      public void cook () 
      {
          .... 
      }
}

  Recipe.name = "Pancake"; 

  Recipe a = new cake Recipe (100, 200, 3, 120, 4); 
  Recipe b = new cake Recipe (100, 200, 1, 120, 5); 

  System.out.println (a.name); / / Displays "Pancake" 

  a.name = "Carrot Pancake"; 
  System.out.println (b.name); / / Displays "Carrot Pancake"
As you can see here, the change was reflected in b.
Reply With Quote
  #3  
Old 05-01-2009
Member
 
Join Date: Nov 2008
Posts: 1,185
Re: The keyword "static", attributes and methods of class in OOP

The robot maker Pancake

Class methods

It is fine all this, but you grew up and you've sold your recipe, it was a great hit! And you have even hired engineers to improve the recipe.
They noticed that the days of full moon, pancakes were a little bland and needed a little more carrots. Not to mention that it allowed people to have a better complexion under the white light of the moon. It was a stroke of good pub and people were happy to have a beautiful complexion through Pancake.

Then you have marketed an intelligent robot capable of producing pancakes with dosages of carrots dependent on the lunar cycle. Just to put lots of eggs, butter, flour, carrots in a small tank and is able to produce your cake with the perfect mix to magnify your complexion.

By using it at home, a little bit young Java decides to statistics on the strengths of the robot depending on the moon. The cake are objects, no problem, but he said that the robot contains the recipe, and that more than it is able to mix depending on the moon. He noted that the robot has a counter that shows how many cakes he created in his life.

Then he said, the robot is the class as it is the recipe, but what's more it counts the number of cakes produced: one has a static variable that number will be recorded in the robot (the recipe). And more it is capable of producing cakes. We will then use a static method. That is a method which is in a class and can be used by everyone.

Note that the robot behaves like a recipe, the only difference is that this is a recipe capable of making itself cakes and the fact that in an intelligent manner.

That is what happens:

Code:
class Recipe
{
      public static String name; 
      public static nbRecipeCount = 0; 

      public int flour; 
      public int sugar; 
      public int eggs; 
      public int butter; 
      public int carrot; 

      public static Recipe CountRecipe (Boolean Fullmoon) 
      {
           Recipe ret; 

           if (Fullmoon) 
               ret = new Recipe (100, 200, 3, 120, 1000); 
          else 
               ret = new Recipe (100, 200, 3, 120, 500); 

           nbRecipeCount ++; 
           return ret; 
      }

      private Recipe (int flour, int sugar, int eggs, int butter, int carrot) 
      {
          this.flour = flour; 
          this.sugar = sugar; 
          this.eggs = eggs; 
          this.butter = butter; 
          this.carrot = carrot; 
      }

      public void cook () 
      {
          .... 
      }
}

  // It creates a full moon cake 
  Recipe a = Recipe.CountRecipe(true);

  // It creates a cake during a crescent moon 
  Recipe b = Recipe.CountRecipe(false);

  System.out.println (b.nbRecipeCount); //Displays 2
We notice several things here. First the constructor is private. Effectively with this robot, it can not establish oneself of pancake with the determination that we want. This is the robot that decides. So you do not have access to the manufacturer for yourself. The robot itself has the right. Actually a static method is part of a class and has access to everything.
Reply With Quote
  #4  
Old 05-01-2009
Member
 
Join Date: Nov 2008
Posts: 1,185
Re: The keyword "static", attributes and methods of class in OOP

What can not be static methods

Here's what can not be a static method:

Code:
static void foo () 
{
    this.c = this.a + this.b;
    ..... 
}
This is wrong, we are in a class and not applicable (for instance).

Imagine that you have the recipe for your cake on the Internet. Using a form, you can change the name of the cake in the recipe.

If you try this:

Code:
static void changeName (String name) 
{
    this.name = name; 
}
This is false. The word "this" represents the current object, ie a cake created from any of the recipe. But you're not in a cake, you're in the recipe. How the recipe can know what cake you refer? There were over a thousand cakes created from this recipe.

The keyword "this" is simply prohibited in a static method.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "The keyword "static", attributes and methods of class in OOP"
Thread Thread Starter Forum Replies Last Post
what are "Overloaded Methods and Constructors" cloud101 Software Development 1 06-02-2012 11:58 PM
Media player classic is throwing error "Class Not Registered" Sheamus Media Player 5 20-05-2011 10:54 AM
<input type="button" value="Enregistrer" onclick="location.href=../asp/PRaces.asp"> Luz Software Development 6 18-05-2010 12:27 AM
Return keyword does not give value of variable "ast" MKAIF Software Development 5 04-03-2010 07:40 PM
Where to use "signal" keyword in SQL ? Balgovind Software Development 2 15-04-2009 01:02 PM


All times are GMT +5.5. The time now is 01:09 AM.