• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

object creation

 
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can i create an object outside a method??? Can i also access variables and methods outside an method??? It give error...please help with this method stuff... am not getting what to write inside and outside the method...
 
author & internet detective
Posts: 41405
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Show the code an the error. Maybe you aren't doing it right.

Also, I changed your subject for you. All caps is considered shouting on the internet.
 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually no "rogue" pieces of executable code are possible.
You can have (unnamed) initializing blocks and assignments but nothing much more.

 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ivan Jozsef Balazs wrote:Actually no "rogue" pieces of executable code are possible.
You can have (unnamed) initializing blocks and assignments but nothing much more.



Yes exactly, technically speaking you cannot write anything outside of the method other than declaration. So everything that you put outside of a function is simply for initialization. It's something that's hidden by the compiler so it can be quite confusing

Think of it in this way:

For dynamic field (instance variables):
is the same as:



For static fields:
is the same as:

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can declare variables outside a method in java. Also, you can create an object and acess variables and methods outside a class. The following example illustrates the same. Suppose you have a class Test and class Sample as follows:


 
Marshal
Posts: 76842
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always use the code button in your posts. I have edited your post to show how much better it looks, and also corrected the spelling mistake: is should be class not Class.
What you are doing is declaring those fields. Field declarations must be outside methods, otherwise they would turn into local variables. You are allowed to combine a declaration with an initialisation in one line. Unfortunately a class which supports such initialisation may suffer from poor design, e.g. non‑private fields.
What you must not do outside methods (or initialiser blocks) is to write statements. It’s all in the Java Language Specification, which is really easy to read.

Challenge: find the one lie in this post
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic