-6

I am learning java programming from http://courses.caveofprogramming.com/courses/java-for-complete-beginners/lectures/38443

This guy, till now have been using the void keyword before declaring any method but once he reached to the passing parameters in methods part, he starting using the public keyword instead of the void keyword. Why did he start using public instead of void?

I have a vague understanding of both the keyword but it would be better if you could explain these keywords to me too.

MartianCactus
  • 159
  • 1
  • 1
  • 8
  • 2
    Just search for `java void` and `java public` in _the internets_. Google is your friend. – Alex Shesterov Jul 09 '16 at 14:45
  • 2
    Please read the duplicate question (link in the comment) and mind that "default" means, that there is no other access modifier specified, like in `void myMethod() {...}`. `public` and `void` are completely different things and you're confused, because that guy there used "default" by omitting an access modifier (that's why the method started with `void` and not `public`). – Tom Jul 09 '16 at 14:46
  • public is an access specifier which means the method can be accessed from anywhere, void is just a type – Nongthonbam Tonthoi Jul 09 '16 at 14:47
  • 1
    It is impossible to use `public` *instead of* `void`. `public` is an access specifier. `void` is a return type, or more specifically the lack of a return type. – Andreas Jul 09 '16 at 15:13
  • More general duplicate: [What does 'public static void' mean in Java?](http://stackoverflow.com/questions/2390063/what-does-public-static-void-mean-in-java) – Pshemo Jul 09 '16 at 15:18

4 Answers4

3

It is similar difference like between small and blue. Both words can be used together but they don't describe same property.

  • public is access modifier - it describes where or who can use/access this portion of code. public means that this code can be used everywhere in Java applications. BTW it doesn't apply only to methods. You can specify type of access also for types (classes, enums, interfaces) and class fields (but you can't use them for local variables, that wound't make sense since their scope is limited to code-block in which they ware declared anyway)
  • void represents methods return type - like sum(int a, int b) should probably return some value which we may want to use somewhere, so we need to specify what is the type of value it will return to make compilers life easier. In case of sum(int a, int b) we could simply set it to also return int like int sum(int a, int b).
    In case of void it means that method doesn't return anything (method simply does something, but we don't expect it to give us something back righ after that, like void turnOffTheLights()). More info in official tutorial: https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

public keyword is an access specifier, which specifies how a piece of code can be accessed. Other access specifiers : private, protected. Access Specifiers

void keyword is a return type, which tells that a method would not return anything on its' completion. Return Types

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
1

public:The keyword public is an access modifier that tells the compiler that the your method is accessible by anyone. you can know more about it here

see this answer to know everything about access specifiers

In Java, difference between default, public, protected, and private

https://stackoverflow.com/a/1020776/5476209

void : The keyword void is a type modifier that states that the method does not return any value. like you have declare any method that does not return any value or anything you can learn about it here

Variables and methods declared as public have access by other variables and methods

public int a;   //this is public variable

public void noReturn()
{
    //this is public method which does not return anything
}

now method which returns value,

 public int a=3;
 public int returnA()
 {
   return a;
 }
Community
  • 1
  • 1
TapanHP
  • 5,969
  • 6
  • 37
  • 66
1

The keywords 'public' and 'void' designate different things about a method; they are not related to the same concept.

A public method is one that can be called from outside the class in which it is declared. There are other designations -- private and protected -- that mean other things; private, for instance, designates a method that can only be called from within the class in which it is declared. The protected and package protected designations are slightly more advanced, you can leave them for later.

The idea behind these is 'information hiding'; the interface of the class is defined by its public members; 'hiding' the other parts simplifies that interface, makes it easier to hide implementation details from callers, so that the class may be extended (and corrected) with less chance of breaking other code.

As for void, a method may return a value, and the type of the value returned is declared with the method. The void keyword indicates that no value is returned from the method.

So it is quite common to have a method declared "public void methodName()', for instance, indicating that it can be called from outside its own class, and that it does not return a value. "public Integer methodName()' can be called from outside the class and returns an Integer instance, and so forth.

The public keyword is also found before variables (though hopefully not often), and in front of a class declaration, with similar meaning. public, private, and protected are referred to as "access modifiers", since they designate from where the affected part of a class may be accessed.

arcy
  • 12,845
  • 12
  • 58
  • 103