Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6384371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:46:04+00:00 2026-05-25T02:46:04+00:00

I’m just beginning to learn Java and I have been very frustrated about learning

  • 0

I’m just beginning to learn Java and I have been very frustrated about learning Java’s scope rules. You see I wish to create a Method without the use of arguments/parameters.

In JavaScript, I can do this with ease using functions:

/** Function to increase 2 vars.
/** **/
function modifyNow(){
 a++;
 b++;
} // END

var a = 5;
var b = 10;

modifyNow();
// By this part, a and b would be 6 and 11, respectively.

Now this is saving me a lot time and simple since whenever the function is called, var a and b, already exist.

So, is there a way to do this in Java without having arguments like how I do it in JavaScript? Or is there another way around this?

Thank you, appreciate the help… ^^

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:46:05+00:00Added an answer on May 25, 2026 at 2:46 am

    You need to use what are called member variables, they are tied to an instance of your class.

    public class MyClass
    { 
    
      // these are Class variables, they are tied to the Class
      // and shared by all instances of the class.
      // They are referenced like MyClass.X
      // By convention all static variables are all UPPER_CASE
      private static int X;
      private static int Y
    
      // these are instance variables that are tied to 
      // instances of the class
      private int a;
      private int b;
    
      /** this the default no arg constructor */
      public MyClass() { this.a = 5; this.b = 10; }
    
      /** this is a Constructor that lets you set the starting values
          for each instance */
      public MyClass(final int a, final int b) { this.a = a; this.b = b; }
    
      public modifyNow() { this.a++; this.b++; }
    
      /** this is an accessor to retrieve the value of a */
      public int getA() { return this.a; }
    
      public int getB() { return this.b; }
     }
    
     final MyClass myInstanceA = new MyClass();
     myInstance.modifyNow();
     // a = 6, b = 11
    
     final MyClass myInstanceB = new MyClass(1, 2);
     myInstance.modifyNow();
     // a = 2, b = 3
    

    Everytime you do a new MyClass() you will get a new independent instance of MyClass that is different from every other instance.

    As you are learning JavaScript is not related to Java in anyway. It is a terrible travesty that they picked that name for marketing reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.