aspose file tools*
The moose likes Beginning Java and the fly likes can class be declared static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can class be declared static?" Watch "can class be declared static?" New topic
Author

can class be declared static?

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

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


http://www.lifesbizzare.blogspot.com || OCJP:81%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Aug 01, 2009
Posts: 1055

but isnt reference to an object possibble if it has a class there must be a class called Scanner
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

ok campbell ,can referenced objects can be declared static,public,private etc
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

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

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Aug 01, 2009
Posts: 1055

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

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Aug 01, 2009
Posts: 1055

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

Joined: Dec 25, 2006
Posts: 60
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

Joined: Aug 01, 2009
Posts: 1055

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

Joined: Dec 25, 2006
Posts: 60
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

Joined: Aug 01, 2009
Posts: 1055

in this case will the assigning of references to the class variables be done during compile time???
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: May 13, 2009
Posts: 679
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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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
 
subject: can class be declared static?