This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can class be declared static?

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can class be declared static
if no then how come
static Scanner=new Scanner(System.in);
is valid
and wats the System.in declared within the parenthesis is it that -- in is a method declared in System class
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not a class you are declaring static, but a reference to a Scanner object. Yes, you can have static references, as you have already seen.

System.in is not a method because it hasn't got enough (). It is a static member of the System class, which is a reference to a Stream which takes input from the keyboard at the command window (unless you change it). The Scanner uses the System.in as a constructor parameter, which tells it where to read from.

You can actually have static classes, but that is something completely different.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but isnt reference to an object possibble if it has a class there must be a class called Scanner
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:but isnt reference to an object possibble if it has a class there must be a class called Scanner


Not getting that properly. Are you asking there should be a class called Scanner? Then there is one in java.util.Scanner
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok campbell ,can referenced objects can be declared static,public,private etc
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara you are right and i think i made a stupid question as objects cannot be initialised without the instance of the class
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:ok campbell ,can referenced objects can be declared static,public,private etc
Yes. But only if they are used as fields. Local variables mustn't be called private, public, static, etc.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
campbell plese illustrate a small example of how to assign public,private,static etc to the referenced objects and not assigining it 2 local variables
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you the scope of a local variable right.....that's why its local local to the method, code block. So if its scope is so limited then there is no need for them to have access or non access modifiers. Well only one non access modifier that is final can be used for local variables. And about class, yes a class can be marked static only if it is a nested class that is inside a class. Top level classes can never be static.

Well class as you say is just a blue print which the JVM uses to create the object when it encounters a 'new' keyword. And that object should be referenced by a live thread(main thread) or from an object(instance references) so that we can use it. So when references are as instance references then we have a choice of making it 1 for the class(static) or 1 for each instance that is created.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:static Scanner=new Scanner(System.in);
That is an example of declaring and initialising a static field of a class. It ought to have been declared "private static Scanner . . . " and ought to have been given a name

With those corrections:
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose i created a class named Abc

class Abc
{
}
class Xyz
{
public static void main(String args[])
{
private Abc a=new Abc();
public Abc a=new Abc();
static Abc a =new Abc();
}

is it correct
 
sujith Acharya
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, since you are declaring instance of class ABC inside the method Main, it will be local variable for that method. As cambell said, local variables can not be public/private/protected/static
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Abc
{
}
class Xyz
{

private Abc a=new Abc();
public Abc a=new Abc();
static Abc a =new Abc();
public static void main(String args[])
{

}
if yes then how will be the variables of class Abc initialised as they are out of the main method
 
sujith Acharya
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code will get complier error, as same variable name is used for more than one member , public ABC a, static ABC a ..etc
If it was different,
The static field will get initiailized when the class XYZ is loaded , and other 2 are initiailized when you create the object of XYZ.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this case will the assigning of references to the class variables be done during compile time???
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well its the instance variables of class xyz of the type ABC. its similar like Integer(wrapper) i. So i is a wrapper of the type Integer. Same goes to String. String is a final class. So if class xyz has String s then its a instance variable of class xyz of type String. Well instance variables can have access modifiers like private public or protected. In a method, we have local variables i.e local to the method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote: . . . how will be the variables of class Abc initialised as they are out of the main method
They will be initialised by the JVM when an instance of the Xyz class is created.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't imagine any need to actually declare a class as static. If you want to treat a class as if it were static, then fill it with static methods and variables, and don't instantiate it.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:I can't imagine any need to actually declare a class as static.

You can declare a class to be static, but it might mean something different than you think it does.

Top-level classes cannot be declared static, but nested classes can. See Nested Classes in Sun's Java Tutorial. The tutorial explains what the difference is between non-static and static nested classes.

In practice, you often want to make a nested class static.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:
Fred Hamilton wrote:I can't imagine any need to actually declare a class as static.

You can declare a class to be static, but it might mean something different than you think it does.

Top-level classes cannot be declared static, but nested classes can. See Nested Classes in Sun's Java Tutorial. The tutorial explains what the difference is between non-static and static nested classes.

In practice, you often want to make a nested class static.


Noted thanks, learn something every day. Indeed, the meaning described by the tutorial was quite different to that which I am familiar with.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic