3 Replies - 211 Views - Last Post: 05 February 2013 - 09:36 PM Rate Topic: -----

#1 small_chick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-July 12

Entry point of a class ?

Posted 05 February 2013 - 07:47 PM

In a class, consider methods, if a method has an access modifier as "Static Void", it will be done when compiling the program without being mandotory to create an instance ! is this true ? ^~^
Is This A Good Question/Topic? 0
  • +

Replies To: Entry point of a class ?

#2 ThrowsException  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 83
  • Joined: 21-February 12

Re: Entry point of a class ?

Posted 05 February 2013 - 08:07 PM

Statics do not have to be void. Static methods can return ints or strings or what ever you want. Static methods belong to a class, not a specific instance of an object. C#'s Math class is a good example of using static methods.
Was This Post Helpful? 0
  • +
  • -

#3 small_chick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-July 12

Re: Entry point of a class ?

Posted 05 February 2013 - 08:27 PM

you can see in a program, there is often a class having a method declared as "static void Main(string[] args)" and this is seen as an entry point of the program !
Was This Post Helpful? 0
  • +
  • -

#4 tesseract36  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 6
  • Joined: 26-January 09

Re: Entry point of a class ?

Posted 05 February 2013 - 09:36 PM

View Postsmall_chick, on 05 February 2013 - 08:27 PM, said:

you can see in a program, there is often a class having a method declared as "static void Main(string[] args)" and this is seen as an entry point of the program !


The main method is special and unique to each program, this is the starting point of the code because we need to define a start somehow... Anyways a static method is a method that only has access to other static methods/ fields of that class and its own variables.

for example :

class account {
static int AddAccounts(account a, account B)/> {
[indent]
return a.getValue() + b.getValue();
[/indent]
}
[indent]
private int value;
public account(int value) {
[indent]
this.value = value;
[/indent]
}
public int getValue() {
[indent]
return this.value;
[/indent]
}
[/indent]
}



In this example the static method addAccounts uses the .getValue() method to get to the variable value because it does not have access to instance variables.
Also if you wanted to from the main method you can use accounts.addAccounts(a, B) but you cannot use accounts.getValue() because getValue needs an instance so you would need to :
account a = new account (500);
a.getValue();


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1